我的ListView背景选择器不会突出显示所选行

时间:2013-02-14 05:27:36

标签: android listview selector

我使用this SO question作为指南,但是当我点击一行时,它不会保持突出显示。我的代码出了什么问题?

score_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/score_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/keyboard"
        android:listSelector="@drawable/selector"
        android:choiceMode="singleChoice"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp" />
...
</RelativeLayout>

抽拉/ selector.xml

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

score_row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scoreRowLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/row_selector" >
...
</LinearLayout>

抽拉/ row_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/highlight" />
    <item android:state_activated="true" android:drawable="@color/highlight" />
    <item android:drawable="@color/transparent" />
</selector>

此代码现在可以正常使用。

1 个答案:

答案 0 :(得分:8)

尝试使用state_activated作为row_selector。