颜色状态列表资源
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
ColorStateList
是一个可以在 XML 中定义并应用为颜色的对象,它实际上是根据所应用到的 View
对象的状态来改变颜色。例如,Button
widget 可以处于几种状态(即按下、聚焦或既不按下也不聚焦)中的一种。您可以使用颜色状态列表,为每种状态提供不同的颜色。
您可以在 XML 文件中描述状态列表。每种颜色都在单个 <selector>
元素内的 <item>
元素中定义。每个 <item>
使用不同的属性来描述其是在什么状态下使用。
在每次状态更改期间,系统将从上到下遍历状态列表,并且将使用与当前状态匹配的第一项。系统的选择并非基于“最佳”匹配,而是基于符合状态的最低标准的第一项。
注意:如果要提供静态颜色资源,请使用简单的颜色值。
- 文件位置:
res/color/filename.xml
文件名用作资源 ID。
- 编译后的资源数据类型:
- 指向
ColorStateList
的资源指针
- 资源引用:
-
在 Java 中:
R.color.filename
在 XML 中:@[package:]color/filename
- 语法:
-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:lStar="floating_point_value"
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>
- 元素:
-
<selector>
- 必需。该元素是根元素。包含一个或多个
<item>
元素。
属性:
xmlns:android
- 字符串。必需。定义 XML 命名空间,即
"http://schemas.android.com/apk/res/android"
。
<item>
- 定义在某些状态下使用的颜色,状态通过其属性来描述。该元素是
<selector>
元素的子元素。
属性:
android:color
- 十六进制颜色。必需。颜色使用 RGB 值和可选的 Alpha 通道指定。
该值始终以井号 (#
) 字符开头,后跟以下某种格式的“透明度、红、绿、蓝”(Alpha-Red-Green-Blue) 信息:
- #RGB
- #ARGB
- #RRGGBB
- #AARRGGBB
android:lStar
- 浮点数。可选。该属性用于修改基本颜色的感知亮度。它既可以采用 0 到 100 之间的浮点值,也可以采用解析为这些值的主题属性。计算某项的整体颜色时,系统会将基础颜色转换为适用于无障碍功能的颜色空间,并将其 L* 设置为
lStar
属性中指定的值。示例:android:lStar="50"
android:state_pressed
- 布尔值。如果是在点按对象时(例如轻触或点击按钮时)使用此项,则为
"true"
;如果是在默认的非点按状态下使用此项,则为 "false"
。
android:state_focused
- 布尔值。如果是在聚焦对象时(例如使用轨迹球或方向键突出显示按钮时)使用此项,则为
"true"
;如果是在默认的非聚焦状态下使用此项,则为 "false"
。
android:state_selected
- 布尔值。如果是在选择对象时(例如打开标签页时)使用此项,则为
"true"
;如果是在未选择对象时使用此项,则为 "false"
。
android:state_checkable
- 布尔值。如果是在对象可勾选时使用此项,则为
"true"
;如果是在对象不可勾选时使用此项,则为 "false"
。仅适用于对象可在可勾选和不可勾选的 widget 之间转换的情况。
android:state_checked
- 布尔值。如果是在对象被勾选时使用此项,则为
"true"
;如果是在对象被取消选择时使用此项,则为 "false"
。
android:state_enabled
- 布尔值。如果是在启用对象(即能够接收轻触或点击事件)时使用此项,则为
"true"
;如果是在停用对象时使用此项,则为 "false"
。
android:state_window_focused
- 布尔值。如果是在应用窗口具有焦点(即应用位于前台)时使用此项,则为
"true"
;如果是在应用窗口没有焦点(例如通知栏下拉或出现一个对话框)时使用此项,则为 "false"
。
注意:系统会应用状态列表中第一个与对象当前状态匹配的项。因此,如果列表中的第一项不包含上述任何状态属性,则每次都会应用该项。因此,请将默认值放在最后,如以下示例所示。
- 示例:
- 保存在
res/color/button_text.xml
的 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>
以下布局 XML 会将颜色列表应用于 View
:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
- 另请参阅:
-
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Color state list resource\n\nA [ColorStateList](/reference/android/content/res/ColorStateList)\nis an object you can define in XML and apply as a color that actually changes colors depending on\nthe state of the [View](/reference/android/view/View) object it is\napplied to. For example, a [Button](/reference/android/widget/Button)\nwidget can exist in one of several states: pressed, focused, or neither. Using a color state list,\nyou can provide a different color for each state.\n\nYou describe the state list in an XML file. Each color is defined in an `\u003citem\u003e` element inside a single `\u003cselector\u003e` element. Each `\u003citem\u003e`\nuses various attributes to describe the state in which it is used.\n\nDuring each state change, the state list is traversed top to bottom, and the first item that\nmatches the current state is used. The selection is *isn't* based on the \"best\"\nmatch, but rather the first item that meets the minimum criteria of the state.\n\n**Note:** If you want to provide a static color resource, use a\nsimple [color](/guide/topics/resources/more-resources#Color) value.\n\nfile location:\n: `res/color/`*filename*`.xml` \n\n The filename is used as the resource ID.\n\ncompiled resource datatype:\n: Resource pointer to a [ColorStateList](/reference/android/content/res/ColorStateList)\n\nresource reference:\n:\n In Java: `R.color.`*filename* \n\n In XML: `@[`*package* `:]color/`*filename*\n\nsyntax:\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cselector xmlns:android=\"http://schemas.android.com/apk/res/android\" \u003e\n \u003citem\n android:color=\"hex_color\"\n android:lStar=\"floating_point_value\"\n android:state_pressed=[\"true\" | \"false\"]\n android:state_focused=[\"true\" | \"false\"]\n android:state_selected=[\"true\" | \"false\"]\n android:state_checkable=[\"true\" | \"false\"]\n android:state_checked=[\"true\" | \"false\"]\n android:state_enabled=[\"true\" | \"false\"]\n android:state_window_focused=[\"true\" | \"false\"] /\u003e\n \u003c/selector\u003e\n ```\n\nelements:\n:\n\n `\u003cselector\u003e`\n : **Required.** This is the root element. Contains one or more `\u003citem\u003e` elements.\n\n Attributes:\n\n `xmlns:android`\n : *String* . **Required.** Defines the XML namespace, which is\n `\"http://schemas.android.com/apk/res/android\"`.\n\n `\u003citem\u003e`\n : Defines a color to use during certain states, as described by its attributes. It is a\n child of a `\u003cselector\u003e` element.\n\n Attributes:\n\n `android:color`\n : *Hexadeximal color* . **Required** . The color is specified with an\n RGB value and optional alpha channel.\n\n The value always begins with a pound (`#`) character, followed by the\n Alpha-Red-Green-Blue information in one of the following formats:\n\n - #*RGB*\n - #*ARGB*\n - #*RRGGBB*\n - #*AARRGGBB*\n\n `android:lStar`\n : *Floating point* . **Optional** . This attribute modifies the base color's perceptual luminance. It takes either a\n floating-point value between 0 and 100 or a theme attribute that resolves as such. The item's\n overall color is calculated by converting the base color to an accessibility friendly color space\n and setting its L\\* to the value specified on the `lStar` attribute.\n\n Example: `android:lStar=\"50\"`\n\n `android:state_pressed`\n : *Boolean* . `\"true\"` if this item is used when the object is tapped, such as when a button\n is touched or clicked. It's `\"false\"` if this item is used in the default, non-tapped state.\n\n `android:state_focused`\n : *Boolean* . `\"true\"` if this item is used when the object is focused, such as when a button\n is highlighted using the trackball or D-pad. It's `\"false\"` if this item is used in the default,\n non-focused state.\n\n `android:state_selected`\n : *Boolean* . `\"true\"` if this item is used when the object is selected, such as when a\n tab is opened. It's `\"false\"` if this item it used when the object isn't selected.\n\n `android:state_checkable`\n : *Boolean* . `\"true\"` if this item is used when the object is checkable. It's `\"false\"` if this\n item is used when the object isn't checkable. Only useful if the object can\n transition between a checkable and non-checkable widget.\n\n `android:state_checked`\n : *Boolean* . `\"true\"` if this item is used when the object is checked. It's `\"false\"` if it\n is used when the object is deselected.\n\n `android:state_enabled`\n : *Boolean* . `\"true\"` if this item is used when the object is enabled, capable of\n receiving touch or click events. It's `\"false\"` if it is used when the object is disabled.\n\n `android:state_window_focused`\n : *Boolean* . `\"true\"` if this item is used when the application window has focus,\n meaning the\n application is in the foreground. It's `\"false\"` if this item is used when the application\n window doesn't have focus, such as if the notification shade is pulled down or a dialog appears.\n\n **Note:** The first item in the state list that\n matches the current state of the object is applied. So, if the first item in the list contains\n none of the preceding state attributes, then it applies every time. For this reason, place your\n default value last, as shown in the following example.\n\n\nexample:\n : XML file saved at `res/color/button_text.xml`: \n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cselector xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003citem android:state_pressed=\"true\"\n android:color=\"#ffff0000\"/\u003e \u003c!-- pressed --\u003e\n \u003citem android:state_focused=\"true\"\n android:color=\"#ff0000ff\"/\u003e \u003c!-- focused --\u003e\n \u003citem android:color=\"#ff000000\"/\u003e \u003c!-- default --\u003e\n \u003c/selector\u003e\n ```\n\n\n The following layout XML applies the color list to a `View`:\n\n ```xml\n \u003cButton\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"@string/button_text\"\n android:textColor=\"@color/button_text\" /\u003e\n ```\n\nsee also:\n:\n - [Color (simple value)](/guide/topics/resources/more-resources#Color)\n - [ColorStateList](/reference/android/content/res/ColorStateList)\n - [State list drawable](/guide/topics/resources/drawable-resource#StateList)"]]