Android app resources-Color State List)

Source: Internet
Author: User

ColorStateList is an object that can define the Applied Color in XML, but it actually changes the color based on the status of the View object using the ColorStateList object. For example, the Button control has one of several different States (press, focus, or none), and uses a color status list. Different colors can be provided during each State.

You can describe the status list in an XML file. Each color is defined in the <item> element inside the <selector> element. Each <item> element uses various attributes to describe the state in which it should be used.

During each state change, the status list is traversed from top to bottom. The first color that is found matches the current state is used. This selection is not based on the best match, instead, select the first project that meets the conditions.

Note: To provide static Color resources, use a simple Color value.

File location ):

Res/color/filename. xml: ID of the resource whose file name is made.

The compiled RESOURCE data type (compliled resource datatype ):

The resource points to a ColorStateList object.

Resource reference ):

In Java code: R. color. filename

In XML: @ [package:] color/filename

SYNTAX (SYNTAX ):

<? Xml version = "1.0" encoding = "UTF-8"?>
<Selectorxmlns: android = "http://schemas.android.com/apk/res/android">
<Item
Android: color = "hex_color"
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>

 

Element (ELEMENTS ):

<Selector>

Required. It must be a root element. Contains one or more <item> elements.

ATTRIBUTES (ATTRIBUTES ):

Xmlns: android:

String, required. It defines the XML namespace, which must be "http://schemas.android.com/apk/res/android ".

<Item>

Defines the color used during a certain State. The color is used as an attribute of this element. It must be a child element of the <selector> element.

ATTRIBUTES (ATTRIBUTES ):

Android: color

Hexadecimal color, required. The color is specified with an RGB value, and the alpha channel can be selected.

This value always starts with #, followed by the Appha-Red-Green-Blue information, in the following format:

# RGB

# ARGB

# RRGGBB

# AARRGGBB

Android: state_pressed

A boolean value. If this item is used when the object is pressed, it must be set to true. (For example, when a button is touched or clicked .) False should be used for the default non-pressed status.

Android: state_focused

A boolean value. If this item is used when the object obtains the focus, it must be set to true. For example, when an option label is opened. If this item is used when the object is not selected, set it to false.

Android: state_checkable

A boolean value. If the project is to be used in the selectable state of the object, it must be set to true. If this item is to be used in an optional State, set it to false. (It is only used to convert an object between optional and optional ).

Android: state_checked

A boolean value. If this item is to be used when the object is selected, it must be set to true. If not, set it to false.

Android: state_enabled

A boolean value. To use this item in the available state of an object (the ability to accept touch or click events), set this item to true. Otherwise, set this item to false.

Android: state_window_focused

A boolean value. If the project is to be used in the focus state of the application window (the application is in the foreground), it must be set to true. Otherwise, it is set to false.

 

Note: Remember that a project in the status list that matches the current status of the object will be used. Therefore, if the first item in the list does not contain any of the preceding state attributes, this item will be used each time. Therefore, the default setting should always be placed at the end.

EXAMPLE ):

The XML file is saved in 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>

The layout XML will apply the color list to a View object:

<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/button_text"
Android: textColor = "@ color/button_text"/>

 
From FireOfStar's column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.