自定义ListView选择器未按预期显示颜色

时间:2015-02-09 17:24:47

标签: android android-listview

我有ListView,显示选择器的默认颜色,如下图所示。

lwasis

我想改变这个选择器的颜色,让我们说颜色青色。

ListView.xml:

<ListView
    android:drawSelectorOnTop="true"
    android:listSelector="@drawable/listitem_selector"
    android:choiceMode="singleChoice"
    android:id="@+id/lw"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

listitem_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/teal" />
    <item android:state_focused="true" android:drawable="@color/teal" />
    <item android:state_selected="true" android:drawable="@color/teal" />
    <item android:state_active="true" android:drawable="@color/teal" />
    <item android:state_activated="true" android:drawable="@color/teal" />
    <item android:state_enabled="true" android:drawable="@color/teal" />
    <item android:state_checked="true" android:drawable="@color/teal" />
    <item android:state_single="true" android:drawable="@color/teal" />
</selector>

正如您所看到的,我尝试了一堆state_*属性但没有成功,所选项目显示为默认颜色。如何更改此外观?

1 个答案:

答案 0 :(得分:1)

我只是在做同样的事情。这对我有用。

@drawable中设置listview,我已将其设置为listview格式,即在布局中放置textViews和checkBoxes以显示在列表视图。

list_view_format.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_view_item">

//your ckeckbox and textviews
</RelativeLayout>

/res/drawable/list_view_item.xml          

<item android:state_pressed="true" >
    <shape>
        <solid android:color="@color/list_item_pressed"/>
    </shape>
</item>

<item android:state_activated="true" >
    <shape>
        <solid android:color="@color/list_item_activated"/>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/list_item_normal" />
    </shape>
</item>

/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="list_item_normal">#96ffdcb5</color>
    <color name="list_item_activated">#ffaa66</color>
    <color name="list_item_pressed">#ffaa66</color>
</resources>