Note:If you want to provide a static color resource,use a simple color value.
In Java: R.color.filename
In XML: @[package:]color/filename
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<item
android: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>
1>selector:颜色源文件的根元素
2>item:定义某一特定状态下的颜色属性包括”state“和color
state:
android:state_pressed 是否按下
android:state_focused 是否取得焦点
android:state_selected 是否被选择
android:state_checkabled 组件是否能被check
android:state_checked 被check了//
android:state_enabled 能够接受触摸或者接触事件
android:state_window_focused 应用程序是否在前台
注:对于不同状态的item,程序会自上而下的匹配,最先匹配的得到程序
XML file saved atres/color/button_text.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/><!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/><!-- focused -->
<item android:color="#ff000000"/><!-- default -->
</selector>
This layout XML will apply the color list to a View:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text"/>
注:同样的方法可以设置View的背景,以drawable Resource代替color Resource也可行。