列表背景的自定义选择器

Joh*_*aum 11 android android-listview

我正在尝试为列表视图中的每个项目设置一些自定义选择器状态.我试过以下"

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@drawable/row_selected_background" />
    <item android:state_activated="true"
          android:drawable="@drawable/row_selected_background" />
    <item android:state_focused="true"
          android:drawable="@drawable/row_selected_background" />
    <item android:drawable="@drawable/row_background" />
</selector>
Run Code Online (Sandbox Code Playgroud)

list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ListView
            android:id="@android:id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/row_background"
            android:listSelector="@drawable/list_selector">

    </ListView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

由于某种原因,列表未选择的背景颜色是定义的.但on on/click始终是列表的默认android holo blue.我究竟做错了什么?

S.D*_*.D. 19

使用android:state_pressed指定的按键状态.


可绘制使用: /res/drawable/bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="@android:color/holo_red_dark"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

选择使用: /res/drawable/item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

列表显示:

    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:saveEnabled="true"
    android:listSelector="@drawable/item"
    />
Run Code Online (Sandbox Code Playgroud)

按下列表项的结果:

在此输入图像描述

注意:列表选择器是在项目视图后面绘制的,项目视图可能有自己的背景.