drawable/listitem_bk.XML
Java代碼:
- <?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/pic1" />
- <!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 -->
- <item android:state_focused="true" android:state_pressed="true"
- android:drawable= "@drawable/pic2" />
- <!-- 觸摸模式下單擊時的背景圖片 -->
- <item android:state_focused="false" android:state_pressed="true"
- android:drawable="@drawable/pic3" />
- <!--選中時的圖片背景 -->
- <item android:state_selected="true"
- android:drawable="@drawable/pic4" />
- <!--獲得焦點時的圖片背景 -->
- <item android:state_focused="true"
- android:drawable="@drawable/pic5" />
- </selector>
listvIEw 的xml文件中加上android:listSelector="@drawable/listitem_bk 或者在XML裡的父視圖中添加屬性android:
background=“@drawable/listitem_bk"
Java代碼:
- Drawable drawable = getResources().getDrawable(R.drawable.picName);
- ListVIEw.setSelector(drawable);