概述:
那么state list drawable(状态图片列表)是什么呢?它是一种定义在xml文件中的图片资源,这种资源可以按照Button当前的状态来动态地选择不同的图片。利用它我们就可以让系统自动改变Button的背景图片,从而使Button实现更好的UI风格。
具体说明:
一个StateListDrawable是一个定义在xml文件中图片资源的对象。它可以根据某些对象不同的状态来从一系列的图片中筛选出一幅图片供这些对象的图片属性使用(形象的说,就好比是一个映射,我们只要将某对象的图片属性设置为这个映射,则这个对象的该图片属性就会在对象不同的状态下被具体指定为不同的图片)。
状态图片列表说明:
res/drawable/filename.xml
这个文件名将被用作资源ID
将生成StateListDrawable类的一个实例
资源标记:
In 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_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
举例 :
XML file saved at res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
以Button为例,展示在布局文件中如何使用上述状态图片列表 This layout XML applies the state list drawable to a Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button" />