做應用時,可能會需要動態改變控件的背景圖片,如果僅僅是簡單的點擊,選中之類的事件,如果靠程序中寫監聽的代碼就顯得太麻煩了,在這種情況下,你可以使用selector動態改變控件背景拉:)
1。在res/drawable目錄下建一個mybutton.xml文件,根據需要,不同的狀態下建立不同的item,並對應相應的圖片 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@color/transparent" /> <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. 注意這句話--> <item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/selector_background_disabled" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/lselector_background_disabled" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/selector_background_transition" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/selector_background_transition" /> <item android:state_focused="true" android:drawable="@drawable/selector_background_focus" /> </selector> 2。在構造layout是引用這個xml <ImageButton android:id="@+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/mybutton"> </ImageButton> 這樣就ok拉 |
android中使用selector動態改變imageView或ImageViewButton的背景
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.