Custom ListView selector not showing color as expected

三世轮回 提交于 2019-12-12 03:16:58

问题


I have a ListView, showing the default color of the selector as shown in the picture below.

I would like to change color of this selector to, lets say the color teal.

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>

As you can see, I've tried a bunch of state_* attributes with no success, the selected item shows as the default color. How do I change this appearance?


回答1:


I'm just doing the same thing. This works for me.

I don't set the @drawable in listview, I have set it in format of listview i.e. in layout where you have put textViews and checkBoxes to be displayed in listview.

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>


来源:https://stackoverflow.com/questions/28415587/custom-listview-selector-not-showing-color-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!