A ColorStateList
is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View
object to which it is applied. For example, a Button
widget can exist in one of several different states (pressed, focused, or neither) and, using a color state list, you can provide a different color during each state.
You can describe the state list in an XML file. Each color is defined in an <item>
element inside a single<selector>
element. Each <item>
uses various attributes to describe the state in which it should be used.
During each state change, the state list is traversed top to bottom and the first item that matches the current state will be used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
Note: If you want to provide a static color resource, use a simple Color value.
-
FILE LOCATION:
-
res/color/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
-
Resource pointer to a
ColorStateList
.
RESOURCE REFERENCE:
-
In Java:
R.color.filename
In XML:@[package:]color/filename
SYNTAX:
-
<?xml version="1.0" encoding="utf-8"?> <selector xmlns: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>
ELEMENTS:
- EXAMPLE:
-
XML file saved at
res/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" />
SEE ALSO:
-
一个
ColorStateList
是一个对象,你可以在XML定义,你可以申请一个颜色,但实际上会改变颜色,根据不同的状态显示
其所适用的对象。例如,一个按钮
控件可以在几种不同的状态(压,突出重点,或两者都不是),并用颜色状态列表中的一个存在,您可以提供每个状态中不同的颜色。你可以这样描述XML文件的状态列表。每种颜色是在定义
的<item>
一个元素中<选择>
元素。每个的<item>
使用不同的属性来描述,其中它应该使用的状态。在每个状态变化,所述状态列表遍历从上到下和相匹配的当前状态将用于-该选择的第一项不基于“最佳匹配”,而仅仅是指满足最低标准的第一项州。
注意:如果您想提供一个静态的颜色资源,使用简单的颜色值。
-
文件位置:
-
水库/颜色/ 文件名 的.xml
文件名 将被用作资源ID。
编译的资源数据类型:
-
资源指针
ColorStateList
。
资源引用:
-
在Java:
。R.color 名
在XML:@ [ 包:]彩色/ 文件名
句法:
-
<?xml的version = "1.0" encoding = "utf-8" ?> < selector xmlns: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>
内容:
- 例:
-
在保存XML文件
RES /彩色/ button_text.xml
:<?xml的 按- > <项目 的android:state_focused = “真正的” 机器人:颜色= “#FF0000FF” /> < -集中- >! <项目 的android:颜色= “#FF000000” /> < -默认- ! > </选择>
这种布局XML将颜色列表应用到一个视图:
<Button android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/button_text" android:textColor = "@color/button_text" />
也可以看看:
-