android drawable 接口,Android 中API之Drawable资源详解及简单实例

Android 中API之Drawable资源

1、最常用的StateListDrawable

说StateListDrawable,很多Android猿可能感到不太熟悉,不过如果说selector选择器,肯定都会恍然大悟,不错,这两个东西就是同一个~~

它的用途之广,每个app必用,下面就写一个demo,来简要说一下用法。

比如一个登陆界面,它的输入框在获取焦点时需要更改背景,登陆按钮在输入框中有内容时,则更改背景颜色,这时候用selector选择器,那就方便多了,效果如下:

6c4b9431b6eb421d3c9dd97aa9cc5e5c.png

b2c51fc0976767b93b7eabc6e278d5a5.png

EditText的背景xml如下:

提交TextView的背景xml如下:

CheckBox的xml如下:

icon_shopping_selected和icon_shopping_unselected是2张图片,下面是CheckBox在activity的布局文件中的设置,如下:

android:id="@+id/cb"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:checked="true"

android:button="@null"

android:drawableLeft="@drawable/cb_agree"

android:padding="20dp" />

之所以把CheckBox的设置单独列出来,是因为这里有个坑。想要自己定制CheckBox的图片,只需要给android:button赋值即可,但为赋值之后,没办法设置padding值,而一般来说,CheckBox给的图片可能会很小,需要设置一些padding。如果将selector选择器设置给button属性,再设置padding,就会造成下面的问题,

058d2b83e71930f5cdbe3c0f5ece036d.png

对应的xml设置如下:

android:id="@+id/cb"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:checked="true"

android:button="@drawable/cb_agree"

android:padding="20dp"

android:background="#00ff00"/>

造成这种情况的原因是,CheckBox是由两部分组成的,一部分是图片ImageView,另一部分是文字内容,想要解决这个问题,按照上面的设置方式即可。

Java代码很简单,如下:

public class StateListDrawableActivity extends Activity{

private TextView mSubmit;

private EditText mPhoneView;

private EditText mPassword;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_state_list_drawable);

mPhoneView = (EditText) findViewById(R.id.et_phone);

mPassword = (EditText) findViewById(R.id.et_password);

BaseTextWatcher watcher = new BaseTextWatcher();

watcher.addEditText(mPhoneView,mPassword);

mSubmit = (TextView) findViewById(R.id.tv_state_list_drawable);

}

class BaseTextWatcher implements TextWatcher{

private ArrayList list = new ArrayList();

public void addEditText(EditText...ets){

for(int i=0;i

ets[i].addTextChangedListener(this);

list.add(ets[i]);

}

}

@Override

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

// TODO Auto-generated method stub

}

@Override

public void onTextChanged(CharSequence s, int start, int before,

int count) {

// TODO Auto-generated method stub

}

@Override

public void afterTextChanged(Editable s) {

for(EditText et:list){

String text = et.getText().toString().trim();

if(TextUtils.isEmpty(text)){

return;

}

}

mSubmit.setEnabled(true);

}

}

}

感谢 阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android,Selector是一个用于定义不同状态下View的样式和背景的XML文件。它可以根据View的不同状态(如按下、选、禁用等)来设置不同的样式和背景。下面是Selector的用法详解实例。 1. 创建selector xml文件 在res/drawable目录下创建一个XML文件,文件名以selector_开头,如selector_button.xml,然后在文件定义不同状态下View的样式和背景。例如: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <!-- 选状态 --> <item android:drawable="@drawable/button_selected" android:state_selected="true" /> <!-- 默认状态 --> <item android:drawable="@drawable/button_normal" /> </selector> ``` 2. 在View应用selector 在布局文件使用android:background属性来应用selector,如: ```xml <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:background="@drawable/selector_button" /> ``` 这样,当Button的状态改变时,它的背景就会自动变化。 除了android:background属性外,还可以在TextView、EditText等View使用android:textColor属性来应用selector,如: ```xml <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="@drawable/selector_text_color" /> ``` 其,selector_text_color.xml文件的内容如下: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:color="@color/colorAccent" android:state_pressed="true" /> <!-- 选状态 --> <item android:color="@color/colorPrimary" android:state_selected="true" /> <!-- 默认状态 --> <item android:color="@android:color/black" /> </selector> ``` 这样,当TextView的状态改变时,它的文字颜色就会自动变化。 以上就是Selector在Android的用法详解实例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值