使用?attr/selectableItemBackground作为背景时,如何修改波纹颜色? [英] How can I modify ripple color when using ?attr/selectableItemBackground as background?

查看:571
本文介绍了使用?attr/selectableItemBackground作为背景时,如何修改波纹颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些SO问题,他们提供了一些可能的方法来实现我想要的目标.例如:

I've seen some SO questions and they gave some possible methods to achieve what I want. For example:

  1. 在styles.xml中使用colorControlHighlight属性.

这是我的样式-v21.xml:

Here is my styles-v21.xml:

<style name="SelectableItemBackground">
    <item name="android:colorControlHighlight">#5677FC</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>

我的小部件:

<TextView
    android:id="@+id/tv_take_photo_as_bt"
    android:layout_width="280dp"
    android:layout_height="48dp"
    android:text="@string/act_take_photo"
    style="@style/SelectableItemBackground"/>

它不起作用.我还尝试将parent="Theme.AppCompat添加到"SelectableItemBackground"样式,或更改为colorControlHighlight(no android: prefix)"或更改为?android:attr/selectableItemBackground,两者均无用.

And it doesn't work. I also tried to add parent="Theme.AppCompat to "SelectableItemBackground" style, or change to colorControlHighlight(no android: prefix)", or change to ?android:attr/selectableItemBackground, neither is useful.

在布局中使用backgroundTint属性.

所以我将android:backgroundTint="#5677FC"添加到我的TextView中.仍然没用.然后,我尝试将android:backgroundTintMode更改为src_insrc_atop,它们从没有改变.

So I add android:backgroundTint="#5677FC" to my TextView. Still useless. Then I tried to change android:backgroundTintMode to src_in and src_atop, and they never make a difference.

因此,当我将?attr/selectableItemBackground用作背景时如何更改波纹颜色.我只关注棒棒糖及以上.预先谢谢你!

So, how can I change ripple color when I use ?attr/selectableItemBackground as background. I only focus on Lollipop and above. Thank you in advance!

推荐答案

最后,我找到了解决方案:我应该写另一种样式,而不是直接在主题SelectableItemBackground中使用android:colorControlHighlight:

Finally I find the solution: instead of using android:colorControlHighlight directly in theme SelectableItemBackground, I should write another style:

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/ripple_color</item>
</style>

然后:

<style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>

最后将style="@style/SelectableItemBackground"添加到layout.xml中的View.

Finally add style="@style/SelectableItemBackground" to View in layout.xml.

2016年8月26日更新 在N发行之后,我发现有时我们不能使用此方法为某种View(例如CardView)设置波纹颜色.现在,我强烈建议开发人员使用RippleDrawable,也可以在xml中声明.这是一个示例:

UPDATED ON 2016/8/26 After N's release, I found that sometimes we cannot use this method to set ripple color for some kind of View(for example, the CardView). Now I highly recommend developers using RippleDrawable, which can also be declared in xml. Here is an example:

我希望在用户触摸/单击API21上方的CardView时显示涟漪效应,当然在棒棒糖之前应该有另一种反馈.所以我应该写:

I want to show a ripple effect when user touches/clicks a CardView above API21, and of course there should be another kind of feedback before Lollipop. So I should write:

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/selectable_item_background"/>

文件夹中的

selectable_item_background:

<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>

drawable-v21 文件夹中的

selectable_item_background:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ripple_black" />
</selector>

最后,在drawable(或drawable-v21)文件夹中的ripple_black:

finally, the ripple_black in drawable(or drawable-v21) folder:

<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".不要忘记为它们设置OnClickListenerOnTouchListener或类似名称,否则波纹不会显示.

That's it. For other views, maybe you should use android:background="@drawable/selectable_item_background". Don't forget to set an OnClickListener, OnTouchListener or something like those for them, otherwise ripple won't show.

这篇关于使用?attr/selectableItemBackground作为背景时,如何修改波纹颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆