主题 : 【代码】Android改变button背景图片使用xml 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-08-02 14:13

 【代码】Android改变button背景图片使用xml

这篇文章我们使用Android XML来改变各种按钮状态(获得焦点、获得焦点并按下、失去焦点、默认)时的背景图,首先我们自己定义一个imgbutton.xml代码:
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 获得焦点时 -->
  4. <item
  5. android:state_focused="true"
  6. android:state_pressed="false"
  7. android:drawable="@drawable/onfocusimage2"
  8. />
  9. <!-- 获得焦点并按下 -->
  10. <item
  11. android:state_focused="true"
  12. android:state_pressed="true"
  13. android:drawable="@drawable/clickimage2"
  14. />
  15. <!-- 失去焦点时 -->
  16. <item
  17. android:state_focused="false"
  18. android:state_pressed="true"
  19. android:drawable="@drawable/clickimage2"
  20. />
  21. <!-- 默认时 -->
  22. <item
  23. android:drawable="@drawable/defaultimage2"
  24. />
  25. </selector>
  26. 在main.xml中定义一个button代码:
  27. <?xml version="1.0" encoding="utf-8"?>
  28. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  29. android:orientation="vertical"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:id="@+id/settingsLayoutId"
  33. android:gravity="center">
  34. <!--这个android:background就读取自己定义的XML文件 -->
  35. <Button
  36. android:background="@anim/imgbutton"
  37. android:text="@string/app_pic"
  38. android:id="@+id/imagesBut"
  39. style="@style/settingsBut"
  40. />
  41. </LinearLayout>
欢迎加入android开发交流群,群号是:314230976