点击Button时,显示不同的效果、背景

  很早以前就知道怎么做,但是觉得最好还是写下来.

  在drawable文件夹下,新建一个文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/co_2" android:state_pressed="true"/>
    <item android:drawable="@drawable/co_2" android:state_focused="true"/>

    <item android:drawable="@drawable/co_1"/>
</selector>

  然后在layout布局里面,给按钮:setBackground。

  解释一下:每个item代表一个约束条件,所以上方文件总共有三个约束条件,  state_pressed、state_focused、分别代表是否按下状态、获得焦点状态,设置了这两个状态,点击按钮时,会改变按钮背景,而在未点击时,背景为:co_1,也就是说,最后一个未标注state的item,表示按钮未点击时的背景.

 

posted @ 2015-09-23 14:17  _启明  阅读(608)  评论(0编辑  收藏  举报