注意 Color State List Resource 与 Color不同,前者是颜色状态表.根据不同状态显示不同颜色,它是State list,里面有多种颜色,后者只是一个颜色.

Color State List Resource

  A 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 Buttonwidget can exist in one of several different states (pressed, focused, or niether) 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
  String. Required. 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 color. Required. 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" />

Drawable(3)Color State List Resource的更多相关文章

  1. [安卓]应用程序资源(App Resources)

    谷歌推荐我们,在开发安卓系统应用程序的时候,要把资源从代码中分离出来,这样便于我们单独维护它们.采取分离的资源设计,我们还可以提供可选资源,支持特定的设备配置譬如不同的语言或屏幕尺寸,随着越来越多的A ...

  2. Android res/目录下子目录详解

    Directory Resource Type animator/ XML files that define property animations. anim/ XML files that de ...

  3. Android 项目中文件夹作用(res文件夹详细介绍)

    1. src:存放所有的*.Java源程序. 2. gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID. 3. assets:可以存放项目一些较大的资源文件,例如: ...

  4. Color About——Second

    下面来简要的说一下Android开发中如何对某一个Activity进行背景色的设置.下面我以名字为FirstActivity的Activity的背景色的设置进行说明,先说一下Drawable类: 关于 ...

  5. Android学习之Drawable(一)

    Drawable有很多种,它们表示一种图像概念,但它们不全是图片.Drawable是什么呢?下面是Google Android API中的定义: A Drawable is a general abs ...

  6. Android Drawable绘图学习笔记(转)

    如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...

  7. Android Drawable 简析

    Drawable 是开发中经常用到的一个概念,我们经常用它去设置 View 的背景,背景可以一个颜色值,也可以是一张资源图片,还可以是一个自定义的 Drawable等等.这篇文章就简单说下 Drawa ...

  8. 安卓权威编程指南-笔记(第21章 XML drawable)

    在Andorid的世界里,凡事要在屏幕上绘制的东西都可以叫drawable,比如抽象图形,Drawable的子类,位图图形等,我们之前用来封装图片的BitmapDrawable就是一种drawable ...

  9. 动态给drawable上色

    只加载一个资源,然后在运行的时候通过ColorFilter进行上色 public Drawable colorDrawable(Resources res, @DrawableRes int draw ...

随机推荐

  1. (转)Quartz.NET管理类

    最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下. public class QuartzManager<T> where T : class,IJob ...

  2. 用PL0语言求Fibonacci数列前m个中偶数位的数

    程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...

  3. java中内部类的定义与访问规则

    java内部类总结 简单来说,内部类就是在我们所熟悉的类中的里面再定义一个类 为什么需要内部类? 当我们描述事物时,事物之中还有事物,我们就用内部类描述事物 因为内部事物在使用外部事物的内容 我举一个 ...

  4. WIX Custom Action (immediate, deffered, rollback)

    Following content is directly reprinted from From MSI to WiX, Part 19 - The Art of Custom Action, Pa ...

  5. ssh连接失败解决方法

    执行如下命令: ssh-keygen -t dsa -P '' -f /etc/ssh/ssh_host_dsa_key ssh-keygen -t rsa -P '' -f /etc/ssh/ssh ...

  6. tomcat错误信息解决方案 严重:StandardServer.await:

    看到这个报错我的第一反应就是端口被占用,用netstat -ant命令查看发现8080端口没有被占用,也可以看到 tomcat的进程已经存在,但是不能对外提供服务. 1.独立运行的tomcat.exe ...

  7. Class.forName、Class.class(属性)、getClass()的区别

    1.出现的时期不同:Class.forName()在运行时加载:Class.class和getClass()是在编译器加载2.Class.class只是在编译时用来存放类的相关信息,并没有实例化对象: ...

  8. 51nod1242 斐波那契数列 矩阵快速幂

    1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...

  9. if (!floor) 小明.跳楼(); 请问小明会在哪些楼层跳楼?

    博客已经迁移到www.imyzf.com,本站不再更新,请谅解! 看到标题请先思考一下这个奇葩的问题..答案在文章最后揭晓.. 会出现这个问题的起源是这样的,一个同学问我: int main() { ...

  10. logback使用笔记

    三大主要元素 looger:记录日志 appender:输出目的地 layout:输出格式 必要步骤: 一.引入包: import org.slf4j.Logger; import org.slf4j ...