ImageButton繼承自ImageView,Button繼承自View。
相比ImageButton,Button具有更強大的功能。
ImageButton僅僅可以指定一個圖片(也可是xml),沒有文字,而Button圖片文字都可以指定。
本測試使用了四個按鈕,分別是普通按鈕btn1;有文字有背景圖(按下時候有變換效果)的按鈕btn2;普通圖片按鈕btn3;有變換效果的圖片按鈕btn4.
測試代碼:
main.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" android:orientation="vertical" > <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="press it" /> <Button android:id="@+id/btn2" android:layout_below="@id/btn1" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="exit" android:background="@drawable/button_sel" /> <ImageButton android:id="@+id/btn3" android:layout_below="@id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/one" android:background="#ffffff"/> <ImageButton android:id="@+id/btn4" android:layout_below="@id/btn3" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_sel" /></RelativeLayout>
button_sel.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/one"/><item android:state_pressed="false" android:drawable="@drawable/two"/></selector>
:
初始狀態: 點擊第二個按鈕exit之後: 點擊第四個按鈕之後: