顏色狀態清單資源
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
ColorStateList
是可以在 XML 中定義的物件,並會套用為實際隨需變更顏色的顏色
得知物件的 View
物件狀態
套用。例如 Button
小工具可能存在以下其中一種狀態:已按下、已聚焦或兩者皆非。使用顏色狀態清單
即可為每個州提供不同顏色
您可以在 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"
表示
如果無法勾選物件,系統就會使用這個項目。只有當物件可以
在可勾選和不可勾選的小工具之間轉換。
android:state_checked
- 「布林值」。如果此項目是在勾選物件時使用,則為
"true"
。"false"
(表示符合條件)
就會在物件取消選取時使用。
android:state_enabled
- 「布林值」。如果此項目是在物件啟用狀態下使用,則為
"true"
,可以
接收觸控或點擊事件。如果是在物件停用時使用,則為 "false"
。
android:state_window_focused
- 「布林值」。如果此項目是在應用程式視窗已聚焦時使用,則為
"true"
;
也就是
應用程式就在前景執行如果是在應用程式使用時,則為 "false"
視窗沒有焦點,例如通知欄向下拉或顯示對話方塊。
注意:狀態清單中的第一個項目
就會與套用的物件目前狀態相符。因此,如果清單中的第一個項目包含
上述任何狀態屬性,則每次都會套用。因此,請將您的
預設值是最後一個值,如以下範例所示。
- 例如:
- XML 檔案儲存在
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>
以下版面配置 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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)"]]