您的位置:首页 > 其它

定义xml为drawable文件并根据状态改变显示的图片资源

2010-03-18 14:40 281 查看
在Button上触摸按下的时候,Button有focused,pressed和default状态,可以使用不同的图片来显示这三种状态。



先定义一个名为btnselector.xml文件,代码如下:



<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item
	  android:state_focused="true"
	  android:state_pressed="false"
	  android:drawable="@drawable/focused"
	></item>
	<item
	  android:state_focused="true"
	  android:state_pressed="true"
	  android:drawable="@drawable/focusedpressed"
	></item>
	<item
	  android:state_focused="false"
	  android:state_pressed="true"
	  android:drawable="@drawable/pressed"
	></item>
	<item
	  android:drawable="@drawable/default"
	></item>
	
</selector>




ImageButton使用btnselector.xml如下:



<ImageButton
      android:id="@+id/stop"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/btnselector"
      android:background="#00000000"
    ></ImageButton>




android:src赋值为"@drawable/btnselector",而不是指向具体的图片资源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐