Android Drawable Resource学习(五)、StateListDrawable

本文详细介绍了StateListDrawable的使用方法,包括如何通过XML文件定义不同状态下的图片资源,以及如何在Java代码中实现相同的功能。此外,还提供了具体的示例代码。

一个StateListDrawable就是一个在xml文件中定义,根据该对象不同的状态,用几张不同的图片来代表相同的图形。比如,一个按钮,有多种状态,获取焦点,失去焦点,点击等等,使用StateListDrawable可以根据不同的状态提供不同的背景。

在XML文件中描述这些状态列表。在唯一的一个<selector>标签下,使用<item>标签来代表一个图形。每个<item>标签使用各种属性来描述它所代表的状态所需要的drawable资源。

再次状态发生改变的时候,都会从上到下遍历这个状态列表,第一个和它匹配的将会被使用-------而不是去选择最适合的匹配。

 

文件位置


 
  1. res/drawable/filename.xml

  2. The filename is used as the resource ID.

编译数据类型:

指向StateListDrawable的指针

资源引用:


 
  1. In Java: R.drawable.filename

  2. In XML: @[package:]drawable/filename

语法:


 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:constantSize=["true" | "false"]

  4. android:dither=["true" | "false"]

  5. android:variablePadding=["true" | "false"] >

  6. <item

  7. android:drawable="@[package:]drawable/drawable_resource"

  8. android:state_pressed=["true" | "false"]

  9. android:state_focused=["true" | "false"]

  10. android:state_hovered=["true" | "false"]

  11. android:state_selected=["true" | "false"]

  12. android:state_checkable=["true" | "false"]

  13. android:state_checked=["true" | "false"]

  14. android:state_enabled=["true" | "false"]

  15. android:state_activated=["true" | "false"]

  16. android:state_window_focused=["true" | "false"] />

  17. </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文件的实现:

 


 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <selector

  3. xmlns:android="http://schemas.android.com/apk/res/android">

  4. <item android:state_focused="true" android:drawable="@drawable/botton_add" />

  5. <item android:state_pressed="true" android:drawable="@drawable/botton_add_down" />

  6. <item android:state_selected="true" android:drawable="@drawable/botton_add" />

  7. <item android:drawable="@drawable/botton_add" />//默认

  8. </selector>


 
  1. <Button

  2. android:background="@drawable/statelist"

  3. android:layout_width="wrap_content"

  4. android:layout_height="wrap_content"/>


Java代码的实现:

 

 


 
  1. Button btn=(Button)findViewById(R.id.btn);

  2. StateListDrawable drawable=new StateListDrawable();

  3. //如果要设置莫项为false,在前面加负号 ,比如android.R.attr.state_focesed标志true,-android.R.attr.state_focesed就标志false

  4.   drawable.addState(new int[]{android.R.attr.state_focused}, this.getResources().getDrawable(R.drawable.botton_add));

  5. drawable.addState(new int[]{android.R.attr.state_pressed}, this.getResources().getDrawable(R.drawable.botton_add_down));

  6. drawable.addState(new int[]{android.R.attr.state_selected}, this.getResources().getDrawable(R.drawable.botton_add));

  7. drawable.addState(new int[]{}, this.getResources().getDrawable(R.drawable.botton_add));//默认

  8. btn.setBackgroundDrawable(drawable);


 
  1. <Button

  2. android:id="@+id/btn"

  3. android:layout_width="wrap_content"

  4. android:layout_height="wrap_content"/>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值