【问题标题】:My ListView background selector won't highlight the selected row我的 ListView 背景选择器不会突出显示选定的行
【发布时间】:2013-02-14 05:27:36
【问题描述】:

我使用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>

drawable/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>

drawable/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>

此代码现在可以正常工作了。

【问题讨论】:

    标签: android listview selector


    【解决方案1】:

    尝试使用state_activated 作为您的 row_selector。

    【讨论】:

    • 如果有人想知道,您还可以使用 listview.clearChoices(); 清除所有选择;我必须使用这个取消选择所有行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2015-09-08
    • 2014-03-22
    相关资源
    最近更新 更多