ColorStateList资源

ColorStateList对象可以在XML中定义,像color一样使用,它能根据它应用到的View对象的状态实时改变颜色。例如,Button可以存在多种状态(pressedfocusedother),如果使用ColorStateList,你就能为它的每个状态提供不同的颜色。

 

你可以在XML文件中描述状态列表。每种颜色定义在一个<item>元素里,<item>放在单个<selector>元素里。每个<item>使用不同的特性来描述在何种状态下使用。

 

当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”,只是符合state的最低标准的第一个item

注意:如果你想提供一个静态的color资源,使用简单的Color值。

 

File Locationres/color/filename.xml(文件名将作为资源ID

Compiled Resource Datatype:指向ColorStateList的资源指针

Resouce ReferenceR.color.filename(Java)@[package:]color/filename(XML)

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_active=["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>

必须。必须是根元素。包含一个或多个<item>元素。

              Attributes

                  xmlns:android

                            String,必须。定义XML的命名空间,必须是

                            http://schemas.android.com/apk/res/android.

    <item>

              定义特定状态的color,通过它的特性指定。必须是<selector>的子元素。

              Attributes

                  android:color

                            16进制颜色。必须。这个颜色由RGB值指定,可带Alpha

                            这个值必须以“#”开头,后面跟随Alpha-Red-Green-Blue信息:

l   #RGB

l   #ARGB

l   #RRGGBB

l   #AARRGGBB

    android:state_pressed

Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

                  android:state_focused

Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。

                  android:state_selected

Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。

                  android:state_checkable

Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

                  android:state_checked

                            Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

                  android:state_enabled

Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

                  android:window_focused

Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。

注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。

 

Examples

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>

 

    这个Layout XML会应用ColorStateList到一个View上:

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/button_text"

    android:textColor="@color/button_text" />

posted on 2010-06-11 10:12  xirihanlin  阅读(9563)  评论(0编辑  收藏  举报