分享

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

 shaobin0604@163.com 2010-05-19

在Button上触摸按下的时候,Button有focused,pressed和default状态,可以使用不同的图片来显示这三种状态。

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

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <selector xmlns:android="http://schemas./apk/res/android">  
  3.     <item  
  4.       android:state_focused="true"  
  5.       android:state_pressed="false"  
  6.       android:drawable="@drawable/focused"  
  7.     ></item>  
  8.     <item  
  9.       android:state_focused="true"  
  10.       android:state_pressed="true"  
  11.       android:drawable="@drawable/focusedpressed"  
  12.     ></item>  
  13.     <item  
  14.       android:state_focused="false"  
  15.       android:state_pressed="true"  
  16.       android:drawable="@drawable/pressed"  
  17.     ></item>  
  18.     <item  
  19.       android:drawable="@drawable/default"  
  20.     ></item>  
  21.       
  22. </selector>  

 

    ImageButton使用btnselector.xml如下:

 

  1. <ImageButton  
  2.   android:id="@+id/stop"  
  3.   android:layout_width="wrap_content"  
  4.   android:layout_height="wrap_content"  
  5.   android:src="@drawable/btnselector"  
  6.   android:background="#00000000"  
  7. ></ImageButton>  

 

   android:src赋值为"@drawable/btnselector",而不是指向具体的图片资源。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多