Android美工坊–listview更改選中時item背景色

來源:互聯網
上載者:User

預設情況下使用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的方式,效果如下

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.