注意 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. union 和 union all 的区别

    Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All  两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...

  2. C# ACM poj1004

    水题.. public static void acm1004(float[] a) { ; foreach (var item in a) { sum += item; } Console.Writ ...

  3. C. Sonya and Queries

    http://codeforces.com/contest/714/problem/C 看到这题目,想想,肯定不能暴力啊,如果用map,如何快速找到满足要求的数目,然后,长度18,我想,这不是熟悉的t ...

  4. CAS原理

    JDK5之前Java是靠synchronized关键字保证同步,这种机制存在以下问题: 在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题 一个线程持有锁会导致其他需要此锁的 ...

  5. cetnos 6.7 安装 oracle 11详解

    CentOS 6.7下Oracle 11g安装详解   1. 安装环境 Linux服务器:CentOS 6.7 64位 Oracle数据库版本:Oracle 11gR2 64位 2. 配置修改及参数优 ...

  6. LinqToExcel常用对象

    1.ExcelQueryFactory对象(1)获取工作表名集合IEnumerable<string> GetWorksheetNames() //获取工作薄中的工作表名 foreach ...

  7. Django基本操作命令

    1.新建一个django项目 django-admin.py startproject project-name 2.新建一个app python manage.py startapp app-nam ...

  8. GPS坐标转百度地图并且加载地图示例.支持微信端访问

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Wx_VehicleLoca ...

  9. 树莓派 raspberry 入门之安装操作系统以及配置

    最近新入手一树莓派,型号是2代B,屏幕是微雪的7 inch c型 显示屏.下面来教大家怎么点亮树莓派. 第一步,装好显示器,显示器的电源接在树莓派的usb口上,HDMI口不多说,连上.然后装好鼠标.键 ...

  10. OAuth在WebApi中的使用,前后台分离的调用方式

    前段时间由于公司架构服务层向WebApi转换,就研究了OAuth在WebApi中的使用,这中间遇到了很多坑,在此记录一下OAuth的正确使用方式. 1.  OAuth是做什么的? 在网上浏览时,大家都 ...