Drawable资源——StateListDrawable 资源
1,认识
StateListDrawable 状态列表
Drawable对象。当使用StateListDrawable作为目标组件的背景、
前景图片时,StateListDrawable对象所显示的Drawable对象会随目标组件状态
的改变而自动切换。是一个可绘制对象中使用几种不同的图像来表示相同的图形,
具体取决于对象的状态的 XML 定义。
创建XML文件:
Android开发过程中,经常对某一View的背景在不同的状态下,设置不同的背景,加强用户体验。如果按钮,在按下时,背景变化,Android为我们提供了 selector背景选择器可以非常方便的解决这一问题。
ML文件中的状态列表,每个图形都由单个<selector>元素中<item>代表。
每个<item>使用各种属性来描述。
定义StateListDrawable对象的XML文件根元素<seletor.../>,该元素可以包含多个<item.../>元素。
文件位置 ︰
res//drawable/filename.xml
语法:
<?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>
2,各属性注解
(1)<selector> 属性
mlns:android
字符串。所需。android的命名空间,
是"http://schemas.android.com/apk/res/android".
android:constantSize
是布尔型,表示固有大小是否不随着其状态的改变而改变,因为状态的改变会 StateListDrawable切换到具体的Drawable,而不同的Drawable具有不同的 大小。false表示各个状态的大小(size)各自不同,true表示所有的状态大小 (以最大的为准)。默认为false。
android:dither
是布尔型。true表示,如果一个屏幕中位图有这不同的像素配置,
位图的抖动。false表示不启用位图的抖动。默认为true。
娘:dither,抖动,是一种故意造成的噪音用以随机化量化误差,
大幅度拉升图像时导致的像banding(色带)这样的问题。
android:variablePadding
是布尔型。选择true时,drawable的内边距会根据状态的变化而变化,
为true时,你必须为不同的状态配置layout,但是通常不建议这么做。
false时,内边距保持一致,所有状态中最大的内边距。默认为false
(2)<item> 属性
android:drawable
属性是必须的,为当前控件指定资源。
android:state_pressed
布尔值。true指当用户点击或者触摸该控件的状态。默认为false
android:state_focused
布尔值。ure指当前控件获得焦点时的状态。默认为false
android:state_hovered
布尔值。ue表示光标移动到当前控件上的状态。默认为false
android:state_selected
布尔值。true表示被选择的状态,例如在一个下拉列表中用方向键下
选择其中一个选项。这个和focus的区别,selected是focus不充分的
情况。比如一个listview获得焦点(focus),
而用方向键选择了其中的一个item(selected)
android:state_checkable
布尔值。ture表示可以被勾选的状态。这个仅在当控件具有被勾选和
不被勾选的状态间转换时才起作用。
android:state_checked
布尔值。true表示当前控件处于被勾选(check的状态)
android:state_enabled
布尔值。true表示当前控件出于可用的状态。比如可以被点击
android:state_activated
布尔值。true表示当前控件被激活的状态。
android:state_window_focused
布尔值。true表示当前控件出于最前端时,应用窗口获得焦点的状态
android:state_first 代表是否处于开始状态
android:state_last 代表是否处于结束状态
android:state_middle 代表是否处于中间状态
注 ︰
请记住,Android 适用对象的当前状态相匹配的状态列表中的第一项。因此,
如果在列表中的第一项包含没有上述的状态属性,它被应用每一次,这就是
为什么你默认值始终应该是最后一次 。
3,示例代码:
(1)state_list_drawable_useing.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 指定获得焦点时的颜色 -->
<item android:state_focused="true"
android:color="#f44"
/>
<!-- 指定失去焦点时的颜色 -->
<item android:state_focused="false"
android:color="#111"
/>
</selector>
(2)各属性写法
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:dither="true"
android:variablePadding="true"
>
<item android:drawable="@drawable/o" android:state_pressed="true"/>
<item android:drawable="@drawable/b" android:state_focused="true"/>
<item android:drawable="@drawable/c" android:state_hovered="true"/>
<item android:drawable="@drawable/d" android:state_selected="true"/>
<item android:drawable="@drawable/e" android:state_checkable="true"/>
<item android:drawable="@drawable/f" android:state_checked="true"/>
<item android:drawable="@drawable/g" android:state_enabled="true"/>
<item android:drawable="@drawable/h" android:state_activated="true"/>
<item android:drawable="@drawable/i" android:state_window_focused="true"/>
</selector>