使用?attr/selectableItemBackground作为背景时,如何修改波纹颜色? [英] How can I modify ripple color when using ?attr/selectableItemBackground as background?
问题描述
我已经看到一些SO问题,他们提供了一些可能的方法来实现我想要的目标.例如:
I've seen some SO questions and they gave some possible methods to achieve what I want. For example:
-
在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_in
和src_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"/>
和
最后,在 finally, the 就是这样.对于其他视图,也许您应该使用 That's it. For other views, maybe you should use 这篇关于使用?attr/selectableItemBackground作为背景时,如何修改波纹颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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
: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"
.不要忘记为它们设置OnClickListener
,OnTouchListener
或类似名称,否则波纹不会显示.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.