步骤
1 放置两张图片ok1和ok2到drawable-mdpi文件夹中
2 在layout文件夹下建立xml文件,如下
<?xml version="1.0" encoding="utf-8"?> <!-- "@drawable/ok1"表示未点击按钮时的图片 --> <!-- "@drawable/ok2"表示点击按钮时的图片 --> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="false" android:drawable="@drawable/ok1" /> <item android:state_pressed="true" android:drawable="@drawable/ok2" /> </selector>
3 main.xml文件如下
<?xml version="1.0" encoding="utf-8"?> <!-- android:background="@layout/ok_selector"图片的背景 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageButton android:id="@+id/ImageButton1" android:background="@layout/ok_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible" /> </LinearLayout>