Color State List Resource

ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied. For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a color state list, you can provide a different color during each state.

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

During each state change, the state list is traversed top to bottom and the first item that matches the current state will be used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.

Note: If you want to provide a static color resource, use a simple Color value.

FILE LOCATION:
res/color/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to a  ColorStateList.
RESOURCE REFERENCE:
In Java:  R.color.filename
In XML:  @[package:]color/filename
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns: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>
ELEMENTS:
<selector>
Required. This must be the root element. Contains one or more  <item> elements.

attributes:

xmlns:android
StringRequired. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
<item>
Defines a color to use during certain states, as described by its attributes. Must be a child of a <selector> element.

attributes:

android:color
Hexadeximal colorRequired. The color is specified with an RGB value and optional alpha channel.

The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB
android:state_pressed
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
android:state_focused
Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.
android:state_selected
Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.
android:state_checkable
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
android:state_checked
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
android:state_enabled
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
android:state_window_focused
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).

Note: Remember that the first item in the state list that matches the current state of the object will be applied. So if the first item in the list contains none of the state attributes above, then it will be applied every time, which is why your default value should always be last (as demonstrated in the following example).

EXAMPLE:
XML file saved at  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>

This layout XML will apply the color list to a View:

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

一个ColorStateList是一个对象,你可以在XML定义,你可以申请一个颜色,但实际上会改变颜色,根据不同的状态显示其所适用的对象。例如,一个按钮控件可以在几种不同的状态(压,突出重点,或两者都不是),并用颜色状态列表中的一个存在,您可以提供每个状态中不同的颜色。

你可以这样描述XML文件的状态列表。每种颜色是在定义的<item>一个元素中<选择>元素。每个的<item> 使用不同的属性来描述,其中它应该使用的状态。

在每个状态变化,所述状态列表遍历从上到下和相匹配的当前状态将用于-该选择的第一项基于“最佳匹配”,而仅仅是指满足最低标准的第一项州。

注意:如果您想提供一个静态的颜色资源,使用简单的颜色值。

文件位置:
水库/颜色/ 文件名 ​​的.xml
文件名 ​​将被用作资源ID。
编译的资源数据类型:
资源指针 ColorStateList
资源引用:
在Java: 。R.color 
在XML: @ [ :]彩色/ 文件名
句法:
<?xml的version = "1.0" encoding = "utf-8" ?> 
< selector  xmlns: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>
内容:
<选择>
必选项。这必须是根元素。包含一个或多个 的<item>元素。

属性:

的xmlns:机器人
字符串必需。定义XML命名空间,它必须是 “http://schemas.android.com/apk/res/android” 
<项目>
定义颜色在某些国家使用,正如它的属性描述。必须是一个的子 <选择>元素。

属性:

机器人:颜色
Hexadeximal颜色必需。颜色与RGB值和可选的alpha通道指定。

该值总是以磅(#)字符开头,然后接着在下面的格式之一阿尔法 - 红 - 绿 - 蓝信息:

  • RGB
  • ARGB
  • RRGGBB
  • AARRGGBB
机器人:state_pressed
布尔。“真”,如果当按下对象应使用此产品的(当按钮被触摸诸如/点击); “假”,如果这个项目应该在默认使用,非按下状态。
机器人:state_focused
布尔。“真”,如果当物体被聚焦(当按钮被使用轨迹球/ D-垫高亮如)应使用这个产品 “假”,如果这个项目应该在默认使用,非聚焦状态。
机器人:state_selected
布尔。“真”如果在选择对象时(如打开一个标签)应该使用这个产品,“假”如果在没有选择的对象应使用此项目。
机器人:state_checkable
布尔。“真”,如果当对象是可勾选应该使用这个产品,“假”,如果当对象不是可勾选此项目应该被使用。(只有有用的,如果该对象可以可检查的和非可检查的小部件之间转换。)
机器人:state_checked
布尔。“真”,如果当对象被选中应该使用这个产品,“假”,如果它应当对象是使用未检查。
机器人:state_enabled
布尔。“真”,如果当物体被启用应使用此产品的(能够接收触摸/点击事件); “假”,如果当物体被禁用它应该被使用。
机器人:state_window_focused
布尔。“真”,如果当应用程序窗口已关注这个项目应该使用(应用程序是在前台),“假”,如果这个项目时,应使用应用程序窗口没有焦点(例如,如果通知栏是拉下来,或者出现一个对话框)。

注意:请记住,在该状态列表相匹配的对象的当前状态中的第一项将被应用。因此,如果在该列表中的第一项包含任何状态的上述属性,那么它会在每个时间,这就是为什么预设值应始终是最后(如下面的示例所示)施加。

例:
在保存XML文件 RES /彩色/ button_text.xml
<?xml的 
 
     
           按- > 
    <项目 的android:state_focused = “真正的” 
          机器人:颜色= “#FF0000FF” />  < -集中- >!
    <项目 的android:颜色= “#FF000000” />  < -默认- ! > 
</选择>

这种布局XML将颜色列表应用到一个视图:

<Button 
    android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content" 
    android:text = "@string/button_text" 
    android:textColor = "@color/button_text"  />
也可以看看:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值