listview隔行背景背景設置

一、首先創建兩個樣式文件,一個news_list_item_background.xml。內容以下: java

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@android:drawable/list_selector_background" android:state_selected="true"/>
    <item android:drawable="@android:drawable/list_selector_background"/>

</selector>
一個 news_list_item_background1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
 
  <item android:state_selected="true" android:drawable="@android:drawable/list_selector_background" /> 
  <item android:drawable="@android:drawable/list_selector_background"/>
</selector>

二、在listview對應的Adaptor類中的getView方法中增長以下代碼: android

if(position%2 == 0){
 convertView.setBackgroundResource(R.layout.news_list_item_background);
 }else{
 convertView.setBackgroundResource(R.layout.news_list_item_background1);
 }  

其中關鍵是xml中的: spa

<item android:drawable="@android:color/white" android:state_pressed="false" android:state_selected="false"/>
相關文章
相關標籤/搜索