Android Drawable Resource学习(五)、StateListDrawable
一个StateListDrawable就是一个在xml文件中定义,根据该对象不同的状态,用几张不同的图片来代表相同的图形。比如,一个按钮,有多种状态,获取焦点,失去焦点,点击等等,使用StateListDrawable可以根据不同的状态提供不同的背景。
在XML文件中描述这些状态列表。在唯一的一个<selector>标签下,使用<item>标签来代表一个图形。每个<item>标签使用各种属性来描述它所代表的状态所需要的drawable资源。
再次状态发生改变的时候,都会从上到下遍历这个状态列表,第一个和它匹配的将会被使用-------而不是去选择最适合的匹配。
文件位置:
- res/drawable/filename.xml
- The filename is used as the resource ID.
res/drawable/filename.xml
The filename is used as the resource ID.
编译数据类型:
指向StateListDrawable的指针
资源引用:
- In Java: R.drawable.filename
- In XML: @[package:]drawable/filename
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
语法:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android"
- android:constantSize=["true" | "false"]
- android:dither=["true" | "false"]
- android:variablePadding=["true" | "false"] >
- <item
- android:drawable="@[package:]drawable/drawable_resource"
- android:state_pressed=["true" | "false"]
- android:state_focused=["true" | "false"]
- android:state_hovered=["true" | "false"]
- android:state_selected=["true" | "false"]
- android:state_checkable=["true" | "false"]
- android:state_checked=["true" | "false"]
- android:state_enabled=["true" | "false"]
- android:state_activated=["true" | "false"]
- android:state_window_focused=["true" | "false"] />
- </selector>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"android:constantSize=["true" | "false"]android:dither=["true" | "false"]android:variablePadding=["true" | "false"] ><itemandroid:drawable="@[package:]drawable/drawable_resource"android:state_pressed=["true" | "false"]android:state_focused=["true" | "false"]android:state_hovered=["true" | "false"]android:state_selected=["true" | "false"]android:state_checkable=["true" | "false"]android:state_checked=["true" | "false"]android:state_enabled=["true" | "false"]android:state_activated=["true" | "false"]android:state_window_focused=["true" | "false"] /></selector>
元素:
<selector>:必须的,必须最为根元素,包含一个或多个<item>元素
-
-
属性
-
xmlns:android
-
字符串。 必须的。定义命名空间,必须是
"http://schemas.android.com/apk/res/android"
。 -
android:constantSize
- 布尔值。内部大小(所有状态中最大的那个)改变时是否报道。默认为false
-
android:dither
- 布尔值。如果位图与屏幕的像素配置不同时,是否允许抖动.(例如:一个位图的像素设置是 ARGB 8888,但屏幕的设置是RGB 565)
-
android:variablePadding
- 布尔值。默认为false,是否要进行绘图填充。(不明白)
-
-
<item>
-
为某个状态定义一个drawable,必须作为<selector>的子元素。
属性:
-
android:drawable
- 必须的,指向一个drawable资源
-
android:state_pressed
- Boolean。是否按下
-
android:state_focused
- Boolean。是否获得获得焦点
-
android:state_hovered
-
Boolean。鼠标在上面滑动的状态。通常和state_focused使用同样的drawable
api14后新增的
-
android:state_selected
- Boolean。是否选中
-
android:state_checkable
- Boolean。是否可以被勾选(checkable)。只能用在可以勾选的控件上
-
android:state_checked
- Boolean。是否被勾选上
-
android:state_enabled
- Boolean。是否可用
-
android:state_activated
-
Boolean。是否被激活并持久的选择
api11后新增
-
android:state_window_focused
- Boolean。当前应用程序是否获得焦点
注意:Android系统将会选中第一条符合当前状态的item。。因此,如果第一项列表中包含了所有的状态属性,那么它是每次就只用他。这就是为什么你的默认值应该放在最后面。
-
-
举例:
XML文件的实现:
- <?xml version="1.0" encoding="UTF-8"?>
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_focused="true" android:drawable="@drawable/botton_add" />
- <item android:state_pressed="true" android:drawable="@drawable/botton_add_down" />
- <item android:state_selected="true" android:drawable="@drawable/botton_add" />
- <item android:drawable="@drawable/botton_add" />//默认
- </selector>
<?xml version="1.0" encoding="UTF-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><item android:state_focused="true" android:drawable="@drawable/botton_add" /><item android:state_pressed="true" android:drawable="@drawable/botton_add_down" /><item android:state_selected="true" android:drawable="@drawable/botton_add" /><item android:drawable="@drawable/botton_add" />//默认
</selector>
- <Button
- android:background="@drawable/statelist"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
<Button android:background="@drawable/statelist"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
Java代码的实现:
- Button btn=(Button)findViewById(R.id.btn);
- StateListDrawable drawable=new StateListDrawable();
- //如果要设置莫项为false,在前面加负号 ,比如android.R.attr.state_focesed标志true,-android.R.attr.state_focesed就标志false
- drawable.addState(new int[]{android.R.attr.state_focused}, this.getResources().getDrawable(R.drawable.botton_add));
- drawable.addState(new int[]{android.R.attr.state_pressed}, this.getResources().getDrawable(R.drawable.botton_add_down));
- drawable.addState(new int[]{android.R.attr.state_selected}, this.getResources().getDrawable(R.drawable.botton_add));
- drawable.addState(new int[]{}, this.getResources().getDrawable(R.drawable.botton_add));//默认
- btn.setBackgroundDrawable(drawable);
Button btn=(Button)findViewById(R.id.btn);StateListDrawable drawable=new StateListDrawable();//如果要设置莫项为false,在前面加负号 ,比如android.R.attr.state_focesed标志true,-android.R.attr.state_focesed就标志falsedrawable.addState(new int[]{android.R.attr.state_focused}, this.getResources().getDrawable(R.drawable.botton_add));drawable.addState(new int[]{android.R.attr.state_pressed}, this.getResources().getDrawable(R.drawable.botton_add_down));drawable.addState(new int[]{android.R.attr.state_selected}, this.getResources().getDrawable(R.drawable.botton_add));drawable.addState(new int[]{}, this.getResources().getDrawable(R.drawable.botton_add));//默认btn.setBackgroundDrawable(drawable);
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
<Buttonandroid:id="@+id/btn" android:layout_width="wrap_content"android:layout_height="wrap_content"/>
使用举例:
http://www.apkbus.com/forum.php?mod=viewthread&tid=52722
Android Drawable Resource学习(五)、StateListDrawable相关推荐
- Android Drawable Resource学习(一)、Drawable Resource简介
Drawable Resource 一个Drawable资源是绘图中的一个普通概念,可以再屏幕上绘制出来.可以通过APIs中的getDrawable(int)方法检索出来,也可以在其他的XML资源中通 ...
- Android OpenGL ES 学习(五) -- 渐变色
OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学 ...
- Android Drawable绘图学习笔记
如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...
- Android Qcom Display学习(五)
该系列文章总目录链接与各部分简介: Android Qcom Display学习(零) 什么是UEFI 高通平台UEFI有关介绍 高通605平台屏调试(4):UEFI初始化流程 Android SDM ...
- android - Drawable - ColorDrawable 学习笔记
ColorDrawable 使用比较简单,就是一个使用单色填写整个画面的 Drawable 对象.使用起来很简单,在 XML 中定义: <resources> <drawable n ...
- Android OpenGL ES 学习(十一) –渲染YUV视频以及视频抖音特效
OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学 ...
- Android OpenGL ES 学习(二) -- 图形渲染管线和GLSL
OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学 ...
- Android OpenGL ES 学习(六) – 使用 VBO、VAO 和 EBO/IBO 优化程序
OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学 ...
- Android OpenGL ES 学习(十) – GLSurfaceView 源码解析GL线程以及自定义 EGL
OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学 ...
最新文章
- swift LOG 输出
- Mybatis 强大的结果映射器ResultMap
- hibernate 多对多(many-to-many)
- php el表达式,JSP EL表达式学习
- OHSUMED数据集介绍
- oracle表空间大小规划,关于oracle表空间的规划方法
- html鼠标点击有手势出来,用原生js+css3撸的一个下拉手势事件插件
- Android缩放比例公式,android开发 缩放到指定比例的尺寸
- 有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?
- Backup Exec 2010 R3 灾难恢复 Exchange 2010
- script中的event和for的意思
- 国内Maven仓库--阿里云Aliyun仓库地址及设置
- BootStrap格栅系统
- 中国土壤厚度空间分布数据
- 【车牌识别】基于matlab APP模板匹配车牌识别(桂贵京粤苏渝)【含Matlab源码 217期】
- java同步异步的区别
- ssh 远程连接详解--(linux运维09)
- 大学抢课python脚本_用彪悍的Python写了一个自动选课的脚本 | 学步园
- 特征值与特征向量及其应用
- android 360全景视频,【Android开发VR实战】二.播放360#176;全景视频(示例代码)
热门文章
- Bribe the Prisoners SPOJ - GCJ1C09C
- 机器学习预测房价 (1)-数据可视化
- 如何两张表关联查询?
- BlockChain:2020年7月10日世界人工智能大会WAIC《链智未来 赋能产业区块链主题论坛》(一)
- 扫地机器人朋友圈文案_有关扫地机器人的文案
- asyncio 警告 DeprecationWarning: There is no current event loop
- 隐藏CNZZ统计代码
- Keil5出现Error: Flash Download failed - Could not load file ‘ces \ces.axf‘的解决方法
- 企业微信 被动回复 没反应问题
- html中粒子风暴代码,怎么用代码打造粒子风暴