Risorsa elenco stato colore
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
ColorStateList
è un oggetto che puoi definire in XML e applicare come colore che cambia effettivamente i colori a seconda
lo stato dell'oggetto View
che è
applicato. Ad esempio, Button
Il widget può essere disponibile in uno dei seguenti stati: premuto, evidenziato o nessuno dei due. Tramite un elenco di stati dei colori,
puoi fornire un colore diverso per ogni stato.
Descrivi l'elenco di stati in un file XML. Ogni colore è definito in un elemento <item>
all'interno di un singolo elemento <selector>
. Ogni <item>
utilizza vari attributi per descrivere lo stato in cui viene utilizzato.
Durante ogni cambio di stato, l'elenco di stato viene spostato dall'alto verso il basso e il primo elemento
corrisponde allo stato corrente. La selezione non si basa sul "migliore"
ma il primo elemento che soddisfa i criteri minimi dello stato.
Nota: se vuoi fornire una risorsa di colore statico, utilizza un
semplice valore color.
- percorso del file:
res/color/filename.xml
-
Il nome file viene utilizzato come ID risorsa.
- tipo di dati della risorsa compilato:
- Puntatore della risorsa a
ColorStateList
- riferimento della risorsa:
-
In Java:
R.color.filename
In XML: @[package:]color/filename
- sintassi:
-
<?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>
- elementi:
-
<selector>
- Obbligatorio. Si tratta dell'elemento principale. Contiene uno o più elementi
<item>
.
Attributi:
xmlns:android
- Stringa. Obbligatorio. Definisce lo spazio dei nomi XML,
"http://schemas.android.com/apk/res/android"
.
<item>
- Definisce un colore da utilizzare durante determinati stati, come descritto dai suoi attributi. È una
secondario di un elemento
<selector>
.
Attributi:
android:color
- Colore esadeximale. Obbligatorio. Il colore è specificato con un
Valore RGB e canale alfa facoltativo.
Il valore inizia sempre con un carattere cancelletto (#
), seguito dal carattere
Informazioni su Alfa-Rosso-Verde-Blu in uno dei seguenti formati:
- #RGB
- #ARGB
- N. RRGGBB
- N. AARRGGBB
android:lStar
- Virgola mobile. Facoltativo. Questo attributo modifica la luminanza percettiva del colore della base. È necessario un
un valore con virgola mobile compreso tra 0 e 100 o un attributo del tema che si risolve come tale. L'elemento
il colore complessivo viene calcolato convertendo il colore di base in uno spazio colore ottimizzato per l'accessibilità
e impostando L* sul valore specificato nell'attributo
lStar
.
Esempio: android:lStar="50"
android:state_pressed
- Booleano.
"true"
se questo elemento viene utilizzato quando viene toccato, ad esempio quando viene toccato un pulsante
quando viene toccato o fatto clic. È "false"
se questo elemento viene utilizzato nello stato predefinito non toccato.
android:state_focused
- Booleano.
"true"
se questo elemento viene utilizzato quando viene attivato lo stato attivo, ad esempio quando viene attivato un pulsante
viene evidenziato usando la trackball o il D-pad. È "false"
se questo elemento viene utilizzato per impostazione predefinita,
in uno stato non incentrato.
android:state_selected
- Booleano.
"true"
se questo elemento viene utilizzato quando viene selezionato l'oggetto, ad esempio quando un oggetto
. È "false"
se questo elemento è stato usato quando non è selezionato.
android:state_checkable
- Booleano.
"true"
se questo elemento viene utilizzato quando è possibile selezionare l'oggetto. È "false"
se questo
viene utilizzato quando l'oggetto non è controllabile. Utile solo se l'oggetto può
transizione tra widget controllabili e non controllabili.
android:state_checked
- Booleano.
"true"
se questo elemento viene utilizzato quando viene selezionato l'oggetto. Se è "false"
viene utilizzato quando l'oggetto è deselezionato.
android:state_enabled
- Booleano.
"true"
se questo elemento viene utilizzato quando l'oggetto è abilitato, in grado di:
ricezione di eventi di tocco o di clic. È "false"
se viene utilizzato quando l'oggetto è disabilitato.
android:state_window_focused
- Booleano.
"true"
se questo elemento viene utilizzato quando la finestra dell'applicazione è attiva,
vale a dire
l'applicazione è in primo piano. È "false"
se questo elemento viene utilizzato quando l'applicazione
finestra non è attiva, ad esempio se l'area notifiche viene visualizzata in basso o viene visualizzata una finestra di dialogo.
Nota: il primo elemento dell'elenco di stati che
corrisponde allo stato corrente dell'oggetto. Quindi, se il primo elemento dell'elenco contiene
nessuno degli attributi di stato precedenti, viene applicata ogni volta. Per questo motivo, posiziona
valore predefinito, come mostrato nell'esempio seguente.
- esempio:
- File XML salvato alle ore
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>
Il seguente file XML di layout applica l'elenco dei colori a un View
:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
- vedi anche:
-
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-27 UTC.
[null,null,["Ultimo aggiornamento 2025-07-27 UTC."],[],[],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)"]]