ListView的item選中後保持選中時的高亮顏色。

今天講解一下當點擊ListView的Item時如何保存點擊後的高亮顏色。

1. ListView默認的點擊背景:

    只要在ListView的Item layout的最外層的佈局上加上 android:background="?android:attr/activatedBackgroundIndicator" 這個即可(要在3.0以上的SDK上纔有該方法);


2. 自定義點擊背景的效果:

    1)在drawable下面定義一個selector(my_background.xml),內容如下:

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:state_activated="true" android:drawable="@drawable/red_dra"></item>
    <item android:state_focused="true" android:drawable="@drawable/red_dra"></item>
    <item android:state_selected="true" android:drawable="@drawable/red_dra"></item>
    <item android:state_pressed="true" android:drawable="@drawable/red_dra"></item>
    <item android:drawable="@color/transparent"></item>

    </selector>


    2)在values/themes.xml下面定義一個style

    <style name="MyBackground">
        <item name="android:activatedBackgroundIndicator">@drawable/red_dra</item>
        </style>


    3)在ListView的item layout的最外層佈局上引用上面的selector和style:

    style="@style/MyBackground"
        android:background="@drawable/my_background"

    備註:red_dra是背景的圖片或者顏色都可以 <drawable name="red_dra">#F00</drawable>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章