Normal:
Focused:
Pressed:
預先準備三個按鈕的圖像, 分別顯示normal, focused 和 pressed 三種狀態, 並且儲存到/res/drawable-mdpi/文件夾中.
在/res/drawable-mdpi/文件夾中創建一個selector.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/focused" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item
android:drawable="@drawable/normal" />
</selector>
修改佈局文件(/res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selector"
/>
</LinearLayout>
除了使用XML, 亦可使用ImageButton.setImageResource加載圖像.
此留言已被作者移除。
回覆刪除