列表视图设置自定义水波纹选择器

12

我正在尝试在Lollipop上使用列表视图控件,满足以下条件:

  1. 主题类型为默认的Theme.Material(深色主题)。
  2. 列表视图包含在具有白色背景的较大布局中。
  3. 列表视图应该有一个与白色背景一起显示的列表选择器。

注意:我被迫使用自定义列表选择器颜色,因为如果我使用白色背景,深色材料主题选择器将使用主题的colorControlHighlight颜色作为涟漪效果,其值为40ffffff,不会显示出来。

我首先尝试了以下内容:

layout xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

列表选择器 xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff0000" >

</ripple>

和列表视图行XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    tools:ignore="UseCompoundDrawables" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/list_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我期望看到的是当我选择一个项目时,该项目会显示为水波纹颜色#ff0000。 然而,我最终看到的是:

enter image description here

我希望的行为与此相近,但应该在所选列表行内完成!我做错了什么?

谢谢, Zach

1个回答

27

您正在使用未限定边界的波纹效果,例如没有内容或蒙版层的波纹效果,因此波纹效果会投射到其父ListView的背景上。您应该设置一个蒙版层来约束波纹效果的边界。

res/drawable/my_list_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item android:id="@android:id/mask">
        <color android:color="@color/white" />
    </item>
</ripple>

3
谢谢Alanv!今天又一次出色的回答(你之前帮我解决了另一个涟漪问题!)。非常感激。 - Zach
@id/mask 是什么?我需要在其他地方定义它吗? - poitroae
应该是 @android:id/mask。已修正代码示例。 - alanv
加入感激的人群) 谢谢,alanv - konunger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接