我尝试在以下条件下在 Lollipop 上使用列表视图控件:
- 主题类型是默认的Theme.Material(深色主题)。
- 列表视图包含在具有白色背景的较大布局内。
- 列表视图应该有一个以白色背景出现的列表选择器。
注意:我被迫使用自定义列表选择器颜色,因为如果我使用白色背景,深色材料主题选择器将使用主题的 colorControlHighlight 颜色作为波纹,即 40ffffff,并且不会显示。
我首先尝试了以下方法:
布局 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 的波纹。然而,这就是我最终看到的:
我所希望的在某种程度上接近这种行为 - 但仅限于选定的列表行!我究竟做错了什么?
谢谢,
扎克
您正在使用无界波纹,例如没有内容或遮罩层的波纹,因此波纹投影到其父 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)