andorid控件改变状态时改变颜色,使用selector。
<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 默认时的背景图片--><item android:drawable="@drawable/pic1" /><!-- 没有焦点时的背景图片--><item android:state_window_focused="false" android:drawable="@drawable/pic1" /><!-- 非触摸模式下获得焦点并单击时的背景图片--><item android:state_focused="true" android:state_pressed="true"android:drawable="@drawable/pic2" /><!-- 触摸模式下单击时的背景图片--><item android:state_focused="false" android:state_pressed="true"android:drawable="@drawable/pic3" /><!--选中时的图片背景--><item android:state_selected="true" android:drawable="@drawable/pic4" /><!--获得焦点时的图片背景--><item android:state_focused="true" android:drawable="@drawable/pic5" /> </selector>
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:color="hex_color"android:state_pressed=["true" | "false"]android:state_focused=["true" | "false"]android:state_selected=["true" | "false"]android:state_checkable=["true" | "false"]android:state_checked=["true" | "false"]android:state_enabled=["true" | "false"]android:state_window_focused=["true" | "false"] /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_selected="true" android:color="#FFF" /><item android:state_focused="true" android:color="#FFF" /><item android:state_pressed="true" android:color="#FFF" /><item android:color="#000" /> </selector>
实例:
先在values目录创建color.xml文件,在里面加入以下自定义颜色(注意不是用color标签)的代码:
<?xml version="1.0" encoding="utf-8"?> <resources><drawable name="red">#f00</drawable><drawable name="green">#0f0</drawable><drawable name="gray">#ccc</drawable> </resources>
然后在res下新建drawable目录,里面新建btn_bg.xml和btn_color.xml文件,代码如下:
btn_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_window_focused="false" android:state_enabled="true"android:drawable="@drawable/btn_test_normal" /><item android:state_enabled="false" android:drawable="@drawable/btn_test_normal" /><item android:state_pressed="true" android:drawable="@drawable/btn_test_press" /><item android:state_focused="true" android:drawable="@drawable/btn_test_normal" /> </selector>
btn_color.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_focused="false" android:state_enabled="true" android:state_pressed="false"android:color="@drawable/red" /><item android:state_enabled="false" android:color="@drawable/gray" /><item android:state_pressed="true" android:color="@drawable/green" /><item android:state_focused="true" android:color="@drawable/red" /> </selector>
最后是测试用的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@android:color/white"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按下文字会变效果"android:textColor="@drawable/btn_color"android:background="@drawable/btn_bg"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮被禁用"android:enabled="false"android:textColor="@drawable/btn_color"android:background="@drawable/btn_bg"/></LinearLayout>
参考:
http://blog.csdn.net/maylian7700/article/details/6978131
http://www.2cto.com/kf/201110/107866.html
http://developer.android.com/guide/topics/resources/color-list-resource.html
http://blog.csdn.net/lzt623459815/article/details/8538784