具有彩色背景和波纹效果的列表视图选择器 [英] Listview selector with colored background and ripple effect
问题描述
android L 开发者预览版中的标准 ListView
选择器使用 colorControlHighlight
实现触摸时的涟漪效果,并且在未聚焦状态下具有透明背景.
Standard ListView
selector in android L developer preview uses colorControlHighlight
for the ripple effect on touch and has a transparent background in unfocused state.
我想定义一个 ListView
项目,它具有彩色背景,并且在触摸时仍然显示具有相同高亮颜色的涟漪效果.现在,如果我定义以下可绘制对象:
I would like to define a ListView
item that has a colored background and still shows the ripple effect on touch with the same highlight color. Now, if I define the following drawable:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="@color/my_background_color"/>
</ripple>
它可以工作,但是波纹从 ListView
项的中间开始,无论触摸位置如何.如果我在 ListView
之外使用相同的背景,例如对于 LinearLayout
,它按预期工作(波纹从触摸位置开始).
it works, but the ripple starts in the middle of the ListView
item, regardless of the touch position. If I use the same background outside of the ListView
, e.g. for a LinearLayout
, it works like expected (the ripple starts on the touch position).
推荐答案
我设法在保持涟漪效应的同时获得单独着色的列表项.使用您拥有的任何适配器设置列表项的背景,并将列表视图设置为在顶部显示选择器:
I've managed to get individually colored list items while maintaining the ripple effect. Set the background of your list items using whatever adapter you have and set the listview to show the selector on top:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true" />
这将在背景上方绘制波纹效果.
This will draw the ripple effect above the background.
这篇关于具有彩色背景和波纹效果的列表视图选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!