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>

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