Android Drawable Resource学习(五)、StateListDrawable
来源:互联网 发布:数据采集费报销内容 编辑:程序博客网 时间:2024/04/28 15:18
一个StateListDrawable就是一个在xml文件中定义,根据该对象不同的状态,用几张不同的图片来代表相同的图形。比如,一个按钮,有多种状态,获取焦点,失去焦点,点击等等,使用StateListDrawable可以根据不同的状态提供不同的背景。
在XML文件中描述这些状态列表。在唯一的一个<selector>标签下,使用<item>标签来代表一个图形。每个<item>标签使用各种属性来描述它所代表的状态所需要的drawable资源。
再次状态发生改变的时候,都会从上到下遍历这个状态列表,第一个和它匹配的将会被使用-------而不是去选择最适合的匹配。
文件位置:
res/drawable/filename.xmlThe filename is used as the resource ID.
编译数据类型:
指向StateListDrawable的指针
资源引用:
In Java: R.drawable.filenameIn 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>
元素:
<selector>:必须的,必须最为根元素,包含一个或多个<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>
<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 android: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学习(五)、StateListDrawable
- Android Drawable Resource学习(五)、StateListDrawable
- Android Drawable Resource学习(五)、StateListDrawable
- Android Drawable Resource学习(五)、StateListDrawable
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Drawable学习之-----StateListDrawable
- Android-drawable资源-StateListDrawable
- Android Drawable Resource学习(四)、LayerDrawable
- Android Drawable Resource学习(六)、LevelListDrawable
- Android Drawable Resource学习(七)、TransitionDrawable
- Android Drawable Resource学习(八)、InsetDrawable
- C++经典面试题
- ZendFramework第二章
- I/O重定向应用例子
- MySql数据库命令导入导出方式
- grep 用法
- Android Drawable Resource学习(五)、StateListDrawable
- 大奖赛计分。
- ZendFramework学习第二章(为变量赋值)
- 对于android2.3源代码的相关修改
- 有关CPU与内存的那些事
- SVN命令
- Linux管理员面试题大全
- Camera保存的图片在Gallery2中找不到
- 决定MyISAM的Select&Insert&Update并发性能的参数