android 按钮Button单击背景切换

res/drawable/btn_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 获得焦点但未按下时的背景图片 -->
    <item android:drawable="@drawable/ic_launcher" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>
    <!-- 按下时的背景图片 -->
    <item android:drawable="@drawable/a" android:state_enabled="true" android:state_pressed="true"/>
    <!-- 按下时的背景图片 -->
    <item android:drawable="@drawable/a" android:state_checked="true" android:state_enabled="true"/>
    <!-- 默认时的背景图片 -->
    <item android:drawable="@drawable/ic_launcher"/>

</selector>

布局中设置按钮背景为btn_selected.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_user_selected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selected" >
    </Button>


</RelativeLayout>

 

posted @ 2013-12-13 23:20  西北野狼  阅读(1122)  评论(0编辑  收藏  举报