最后我找到了解决方案:而不是使用android:colorControlHighlight
直接在主题中SelectableItemBackground
,我应该写另一种风格:
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/ripple_color</item>
</style>
Then:
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
最后添加style="@style/SelectableItemBackground"
to View
在布局.xml 中。
更新于 2016/8/26N发布后,我发现有时我们无法使用此方法来设置某些类型的波纹颜色View
(例如,CardView
)。现在我强烈推荐开发者使用RippleDrawable
,也可以在 xml 中声明。这是一个例子:
我想在用户触摸/单击时显示连锁反应CardView
API21以上,当然在Lollipop之前应该还有另一种反馈。所以我应该写:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/selectable_item_background"/>
and selectable_item_background
in drawable
folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:drawable="@color/color_clicked" />
</selector>
selectable_item_background
in drawable-v21
folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ripple_black" />
</selector>
最后,ripple_black
in drawable
(or drawable-v21
) 文件夹:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/color_clicked"
tools:ignore="NewApi" /> <!--you can remove this line if it's in v21 folder-->
就是这样。对于其他视图,也许您应该使用android:background="@drawable/selectable_item_background"
。不要忘记设置一个OnClickListener
, OnTouchListener
或类似的东西,否则波纹不会显示。