问题
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