用xml来定义imagebutton的状态

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

    <item android:drawable="@drawable/button1" 
        android:state_focused="true" 
        android:state_pressed="false"/>
    <item android:drawable="@drawable/button2" 
        android:state_focused="true" 
        android:state_pressed="true"/>
    <item android:drawable="@drawable/button3" 
        android:state_focused="false" 
        android:state_pressed="true"/>
    <item 
        android:drawable="@drawable/button1"/>
</selector>

button1,button2,button3等是图片的名字,放在res/drawable-hdpi位置.

res/layout/main.xml
<ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="61dp"
        android:layout_y="280dp"
        android:src="@drawable/drawable_x"
        />

选择器文件名:android:src="@drawable/ drawable_x"

你可能感兴趣的:(用xml来定义imagebutton的状态)