منبع فهرست حالت رنگ
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
ColorStateList
شی ای است که می توانید آن را در XML تعریف کنید و به عنوان رنگی اعمال کنید که در واقع رنگ ها را بسته به وضعیت شی View
که روی آن اعمال می شود تغییر می دهد. برای مثال، یک ویجت Button
میتواند در یکی از چندین حالت وجود داشته باشد: فشرده، متمرکز، یا هیچکدام. با استفاده از فهرست حالت رنگ، می توانید برای هر حالت رنگ متفاوتی ارائه دهید.
شما لیست وضعیت را در یک فایل XML توصیف می کنید. هر رنگ در یک عنصر <item>
در داخل یک عنصر <selector>
تعریف میشود. هر <item>
از ویژگی های مختلفی برای توصیف وضعیتی که در آن استفاده می شود استفاده می کند.
در طول هر تغییر حالت، لیست حالت از بالا به پایین پیمایش می شود و اولین موردی که با وضعیت فعلی مطابقت دارد استفاده می شود. انتخاب بر اساس "بهترین" تطابق نیست ، بلکه اولین موردی است که حداقل معیارهای ایالت را برآورده می کند.
توجه: اگر می خواهید یک منبع رنگ ثابت ارائه کنید، از یک مقدار رنگ ساده استفاده کنید.
- محل فایل:
-
res/color/ filename .xml
نام فایل به عنوان شناسه منبع استفاده می شود. - نوع داده منبع کامپایل شده:
- اشاره گر منبع به
ColorStateList
- مرجع منبع:
- در جاوا:
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 و کانال آلفا اختیاری مشخص می شود.
مقدار همیشه با یک کاراکتر پوند ( #
) شروع می شود و به دنبال آن اطلاعات آلفا-قرمز-سبز-آبی در یکی از قالب های زیر قرار می گیرد:
- # RGB
- # ARGB
- # RRGGBB
- # AARRGGBB
-
android:lStar
- نقطه شناور . اختیاری این ویژگی درخشندگی ادراکی رنگ پایه را تغییر می دهد. یا یک مقدار ممیز شناور بین 0 تا 100 یا یک ویژگی تم می گیرد که به این ترتیب حل می شود. رنگ کلی مورد با تبدیل رنگ پایه به فضای رنگی مناسب برای دسترسی و تنظیم L* آن بر روی مقدار مشخص شده در ویژگی
lStar
محاسبه می شود. مثال: android:lStar="50"
-
android:state_pressed
- بولی .
"true"
اگر از این مورد در هنگام ضربه زدن به شی استفاده شود، مانند زمانی که دکمه ای لمس یا کلیک می شود. اگر این مورد در حالت پیشفرض و بدون ضربه استفاده شود، "false"
است. -
android:state_focused
- بولی .
"true"
اگر این مورد در هنگام فوکوس کردن شی مورد استفاده قرار گیرد، مانند زمانی که یک دکمه با استفاده از گوی یا پد D برجسته می شود. اگر این مورد در حالت پیشفرض و بدون تمرکز استفاده شود، "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" />
- همچنین ببینید:
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]