Drawable(3)Color State List Resource
注意 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 Button
widget 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
- COMPILED RESOURCE DATATYPE:
- Resource pointer to a
ColorStateList
. - RESOURCE REFERENCE:
- In Java:
R.color.filename
@[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>
<item>
elements.
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.<item>
<selector>
element.- attributes:
android:color
- 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
android:state_focused
android:state_selected
android:state_checkable
android:state_checked
android:state_enabled
android:state_window_focused
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的更多相关文章
- [安卓]应用程序资源(App Resources)
谷歌推荐我们,在开发安卓系统应用程序的时候,要把资源从代码中分离出来,这样便于我们单独维护它们.采取分离的资源设计,我们还可以提供可选资源,支持特定的设备配置譬如不同的语言或屏幕尺寸,随着越来越多的A ...
- Android res/目录下子目录详解
Directory Resource Type animator/ XML files that define property animations. anim/ XML files that de ...
- Android 项目中文件夹作用(res文件夹详细介绍)
1. src:存放所有的*.Java源程序. 2. gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID. 3. assets:可以存放项目一些较大的资源文件,例如: ...
- Color About——Second
下面来简要的说一下Android开发中如何对某一个Activity进行背景色的设置.下面我以名字为FirstActivity的Activity的背景色的设置进行说明,先说一下Drawable类: 关于 ...
- Android学习之Drawable(一)
Drawable有很多种,它们表示一种图像概念,但它们不全是图片.Drawable是什么呢?下面是Google Android API中的定义: A Drawable is a general abs ...
- Android Drawable绘图学习笔记(转)
如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...
- Android Drawable 简析
Drawable 是开发中经常用到的一个概念,我们经常用它去设置 View 的背景,背景可以一个颜色值,也可以是一张资源图片,还可以是一个自定义的 Drawable等等.这篇文章就简单说下 Drawa ...
- 安卓权威编程指南-笔记(第21章 XML drawable)
在Andorid的世界里,凡事要在屏幕上绘制的东西都可以叫drawable,比如抽象图形,Drawable的子类,位图图形等,我们之前用来封装图片的BitmapDrawable就是一种drawable ...
- 动态给drawable上色
只加载一个资源,然后在运行的时候通过ColorFilter进行上色 public Drawable colorDrawable(Resources res, @DrawableRes int draw ...
随机推荐
- 40个Java集合面试问题和答案【中】【转载】
接上文:http://www.cnblogs.com/xujianbo/p/5148075.html 16.UnsupportedOperationException是什么? Unsupporte ...
- @Transactional详解
@Transactional spring 事务注解 默认遇到throw new RuntimeException("...");会回滚需要捕获的throw new Exc ...
- C语言变参函数/Variadic fucntion
几个重要的 宏/类型 定义 Macros Defined in header <stdarg.h> va_start enables access to variadic function ...
- N的N次方(高校俱乐部)
最近一直在刷字符串和线段树,也越来越少玩高校俱乐部,无聊看到一题N的N次方的问题,脑海中各种打表就涌现出来了. 弄了不一会儿,就写完了,马上提交,但是系统好像出了问题,提示"哦哦,出了点状况 ...
- hdu 1063 Exponentiation (高精度小数乘法)
//大数继续,额,要吐了. Problem Description Problems involving the computation of exact values of very large m ...
- 安装Debian 7.8 过程,以及该系统的配置过程
最近,我给自己的电脑安装了Debian操作系统,版本7.8 stable.Debian我相信大家都能了解,它是Ubuntu的基础,Ubuntu是基于Debian的unstable开发的.我安装这个系统 ...
- linux下tomcat的安装
本文主要内容: (1)安装apr,这是 Apache 为了提升 Tomcat 的性能搞的一套本地化 Socket, Thread, IO 组件也就是说它有高级 IO 功能, 操作系统级别的功能调用, ...
- PHP初学留神(三)
星期一进行面试结束后,意味着我的考研日子也结束了,以及我的2013.在好好总结之后还不能停止学习,心想着要把算法继续学下去,还有Linux.不过呢,始终都要记住尼采老师的这句当头棒喝:“不加选择的知识 ...
- 如何实现一个malloc函数
一.概述 1.malloc简介 函数所在头文件:<stdlib.h> 函数原型是:void *malloc (size_t n) 函数功能:在内存的动态存储区中分配一个长度为size的连续 ...
- Unity3d Shader开发(三)Pass(Blending )
混合被用于制作透明物体. 当图像被渲染时,所有着色器被执行以后,所有贴图被应用后,像素将被写到屏幕.他们是如何通过Blend命令的控制和已有的图像合并呢? Syntax 语法 Blend Off Tu ...