來完成,但相對來說較爲麻煩,還可通過selector來完成。
1.在drawable文件夾下創建一xml文件,名稱隨意,但一定要在drawable下,如my_button_background.xml
內容如下:
[color=red][b]<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="@drawable/沒有焦點時的圖片背景" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable=
"@drawable/非觸摸模式下獲得焦點並單擊時的背景圖片" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/觸摸模式下單擊時的背景圖片" />
<item android:state_selected="true"
android:drawable="@drawable/選中時的圖片背景" />
<item android:state_focused="true"
android:drawable="@drawable/獲得焦點時的圖片背景" />
</selector>[/b]
2.在定義ImageButton時,設置背景屬性時引用
[b]<ImageButton
android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_button_background"><!--這是關鍵,要引用上面xml所生成的id-->
</ImageButton> [/b][/color]
OK,以上操作完成,即可實現按鈕圖片改變,但是在實驗時,開發環境爲sdk1.6,只有ImageButton實驗成功,ImageView實驗失敗,可能是不支持吧!
以上內容參照:
[url]http://hi.baidu.com/linrw/blog/item/69e1eb89b516aabe0f2444e5.html/cmtid/81d88bd5be5973ca51da4b16[/url]
[url]http://hi.baidu.com/pengwenfu/blog/item/a8acaaa5c4ce5bfb9152ee86.html/cmtid/17f9b028b4f977f098250a67[/url]