资源文件之StateListDrawable

StateListDrawable是定义在XML中的Drawable对象,能根据状态来呈现不同的图像。例如,Button存在多种不同的状态(pressedfocusedother),使用StateListDrawable,你可以为Button的每个状态提供不同的按钮图像。

你可以在XML文件中描述状态列表。在<selector>元素里的每个<item>代表每个图像。每个<item>使用不同的特性来描述使用的时机。

当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”,只是符合state的最低标准的第一个item

Syntax

<?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_active=["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>元素。

Attributes

xmlns:android

String。必须。定义XML的命名空间,必须是

http://schemas.android.com/apk/res/android”。

android:constantSize

Boolean。“true”表示随着状态变化,Drawable的大小保持不变(所有状态中最大的size);“false”表示大小会变化。默认是false

android:dither

Boolean。“true”表示当Bitmap和屏幕的不是相同的像素设定时支持Bitmap抖动(例如,ARGB 8888BitmapRGB 565的屏幕);“false”表示不支持。默认是“true”。

android:variablePadding

Boolean。“true”表示DrawablePadding可以变化;“false”表示Padding保持相同(所有状态的最大Padding)。使能这一特征需要在状态变化时处理Layout,一般都不支持。默认值是false

<item>

定义特定状态的Drawable,通过它的特性指定。必须是<selector>的子元素。

Attributes

android:drawable

Drawable资源。必须。指向一个Drawable资源。

android:state_pressed

Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

android:state_focused

Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。

android:state_selected

Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。

android:state_checkable

Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

android:state_checked

Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

android:state_enabled

Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

android:window_focused

Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。

注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。

Example

XML文件保存在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>

Layout XML将这个Drawable应用到一个View上:

<ImageView

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:src="@drawable/button" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值