listview更改選中時item背景色(轉)


默認情況下使用ListView背景色是黑色,選中item的高亮顏色是菊黃色,很多時候不得不自己定義背景色或者背景圖

android:cacheColorHint="@android:color/transparent",意思為去黑色底色,比如ListView滾動時會刷新界面,默認顏色還是系統顏色,所以采用這種方式設置其為透明即可,這個屬性在ListView中使用圓角圖片來設置ListView時很有用

android:divider="@null"用於去掉listview的item之間的黑線

1、背景色

即在list_item_color_bg.xml中通過設置color來實現點擊item時不同的顏色,但是如果使用color的 話,listview無法使用android:listSelector屬性,如果設置android:listSelector方式的話,點擊一個 item后整體的ListView全部都會變成一種顏色,這時必須采用在item中設置android:background的方式才可以。 android:listSelector方式適用於圖片的方式,即類似與(android:drawable="@drawable/img")

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

color.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#ffffff</color> <color name="black">#000000</color> <color name="green">#00ff00</color> </resources>

下面再看看布局文件

listview.xml,用color的方式,這里不能使用listSelector

復制代碼
<?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" > <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fastScrollEnabled="true" android:cacheColorHint="@android:color/transparent" android:divider="@null" /> </LinearLayout>
復制代碼

list_item_color.xml,通過color設置直接在item的布局中設置背景即可

復制代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="@drawable/list_item_color_bg">  <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" /> </LinearLayout> </LinearLayout>
復制代碼

效果圖

2、背景圖

這種方式是在selector文件中采用圖片來設置item的背景,無論是設置ListView的android:listSelector的方式 還是設置item的android:background的方式都可以使用,不過最好還是使用android:background的方式,因為使用 android:listSelector的方式時下面的selector文件中設置的默認時的圖片

<item android:drawable="@drawable/login_input"/>)不會顯示,而改為background的方式則可以。有些奇怪,希望懂的能指點一下

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

listView此時設置如下,這里在item中不設置android:background

復制代碼
<?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" > <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fastScrollEnabled="true" android:cacheColorHint="@android:color/transparent" android:listSelector="@drawable/list_item_drawable_bg"  /> </LinearLayout>
復制代碼

此時的效果圖如下:背景圖是.9.png圖片,注意默認的白色.9.png圖片login_input沒有顯示

如果使用android:background的方式,取消android:listSelector的方式,效果如下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM