注意 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. 自定义popupmenu菜单背景色

    procedure TForm1.N11DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean); be ...

  2. 通过自关联替代开窗函数实现SQL优化

    数据库环境:SQL SERVER 2005 我们平时都是通过开窗析函数来取代自关联,减少表扫描,从而优化SQL.今天,反其道而行, 用自关联改写开窗函数.我们先来看一下SQL. 原SQL语句 SELE ...

  3. angularJs中ui-router的使用

    学习使用angular中,ui-route是其中的一个难点,简单使用没什么问题,但涉及到多级嵌套,就感觉有茫然,查了很多资料,踩过很多坑,到目前为止也不能说对ui-route有全面了解:这里只是把填补 ...

  4. 程序员面试题精选100题(38)-输出1到最大的N位数[算法]

    作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. 分析:这 ...

  5. 改善EF代码的方法(下)

    本节,我们将介绍一些改善EF代码的方法,包括编译查询.存储模型视图以及冲突处理等内容. > CompiledQuery 提供对查询的编译和缓存以供重新使用.当相同的查询需要执行很多遍的时候,那么 ...

  6. phpstudy 下开启openssl

    1.在php.ini里面的extension=php_openssl.dll是开启的状态,没有被注释. 2.里面的allow_url_fopen = On表示开启了. 3.如果你是Windows 64 ...

  7. sql server 2008数据复制方法

    发布订阅份为两个步骤:1.发布.2.订阅.首先在数据源数据库服务器上对需要同步的数据进行发布,然后在目标数据库服务器上对上述发布进行订阅.发布可以发布一张表的部分数据,也可以对整张表进行发布.下面分别 ...

  8. 开发APP不搞清楚这20个问题,必然沦为一场灾难

    移动经济的高速增长极大刺激了企业和个人的APP开发热情,从卖野山鸡的到卖无人机的,从老大妈到小正太都跃跃欲试,更不要说那些传统企业的信息主管们了. 面对今天如过江之鲫的APP市场,很少有人意识到,移动 ...

  9. 巧妙使用checkbox制作纯css动态导航栏

    前提:很多时候.我们的网页都需要一个垂直的导航栏.可以分类.有分类.自然就有展开.关闭的功能.你还在使用jquery操作dom来制作吗?那你就out了! 方案:使用checkbox 的 checked ...

  10. php设计模式-------(1)策略模式

    一.为什么我要学习设计模式. 我的上一个项目是做App接口,由于时间紧,老板催的急,所以到最后项目完工时发现居然写了几万行代码,可想而知代码质量有多糟糕.而且很多时候,调用接口的开发人员来找我说某个接 ...