Listview选择器错误的背景

时间:2014-02-21 00:39:10

标签: android listview colors background

在API7那里我遇到的问题是,如果我选择一行,背景会改变,而不是我选择的行。我的错误在哪里? 使用Android 4的手机,颜色没问题,怎么回事?

list_selector_flatcolor.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_row_default_bg" android:state_pressed="false" android:state_activated="true"/>
    <item android:drawable="@color/list_row_pressed_bg" android:state_pressed="true"/>
    <item android:drawable="@color/list_row_selected_bg" android:state_pressed="false" android:state_activated="true"/>

</selector>

使用listview进行布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/custom_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_selector_flatcolor"
        android:dividerHeight="1dp"/>

</LinearLayout>

colors.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_color_default">#00000C</color>
    <color name="text_color_inverse">#FFFFFF</color>
    <color name="white">#FFFFFF</color>
    <color name="list_row_default_bg">#ffd30210</color>
    <color name="list_row_pressed_bg">#008cef</color>
    <color name="list_row_selected_bg">#86d3f6</color>
</resources>

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个selector.xml。您需要添加android:state_selected州。

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

如果您使用Custom Adapter,则可以将此选择器设置为Background Custom Layout,如:

android:background="@drawable/selector"