مورد قائمة حالة الألوان
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
ColorStateList
عبارة عن كائن يمكنك تعريفه في XML وتطبيقه كلون يتغيّر الألوان بالفعل اعتمادًا على
حالة الكائن View
التي تم تطبيق البيانات عليها. على سبيل المثال، Button
يمكن أن توجد الأداة بإحدى الحالات المتعددة: بالضغط أو التركيز أو عدم وجود أي منهما. باستخدام قائمة حالة الألوان،
يمكنك توفير لون مختلف لكل ولاية.
أنت تصف قائمة الحالات في ملف XML. يتم تحديد كل لون في عنصر <item>
داخل عنصر <selector>
واحد. كل <item>
يستخدم سمات مختلفة لوصف الحالة التي يتم استخدامه فيها.
أثناء كل تغيير للحالة، يتم اجتياز قائمة الحالة من أعلى إلى أسفل، ويكون العنصر الأول
تطابق الحالة الحالية التي يتم استخدامها. الاختيار لا يستند إلى "الأفضل"
ولكن بالأحرى العنصر الأول الذي يفي بالحد الأدنى من معايير الولاية.
ملاحظة: إذا كنت تريد توفير مورد ألوان ثابتة، استخدِم السمة
قيمة اللون البسيطة.
- مكان الملف:
res/color/filename.xml
-
يتم استخدام اسم الملف كمعرّف المورد.
- نوع بيانات الموارد المجمّعة:
- مؤشر المورد إلى
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
- لون سداسي عشري: مَعلمة مطلوبة. يتم تحديد اللون باستخدام
قيمة النموذج اللوني أحمر أخضر أزرق وقناة ألفا اختيارية.
تبدأ القيمة دائمًا بحرف جنيه (#
)، متبوعًا
معلومات ألفا-الأحمر-الأخضر-الأزرق بأحد التنسيقات التالية:
- #نموذج أحمر أخضر أزرق
- #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"
إذا تم استخدام هذا العنصر عند استخدام التطبيق.
لا تحتوي النافذة على تركيز، مثلاً عند سحب مركز الإشعارات للأسفل أو ظهور مربّع حوار.
ملاحظة: العنصر الأول في قائمة الحالات
تطابق الحالة الحالية للكائن. لذلك، إذا كان العنصر الأول في القائمة يحتوي على
لا شيء من سمات الحالة السابقة، فإنه يتم تطبيقها في كل مرة. لهذا السبب، ضع
القيمة الافتراضية last، كما هو موضح في المثال التالي.
- مثلا:
- تم حفظ ملف 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 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 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)"]]