棒棒堂的backgroundTint有一个按钮没有任何影响 [英] Lollipop's backgroundTint has no effect on a Button
问题描述
我在我的活动一个按钮,我想它有我的主题的强调色。
相反,像我们所要做的pre-棒棒糖让我自己可绘,自然我想使用新的 backgroundTint
属性。
I have a Button in my Activity, and I'd like it to have my theme's accent color.
Instead of making my own drawables like we had to do pre-Lollipop, naturally I'd like to use the new backgroundTint
attribute.
<Button
android:id="@+id/btnAddCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/accent"
android:text="@string/addressInfo_edit_addCode" />
不幸的是,没有任何效果,此按钮保持灰色。
Unfortunately it has no effect, the button stays gray.
我尝试了不同的值 backgroundTintMode
,它没有改变任何东西。
I tried different values for backgroundTintMode
, which didn't change anything.
我也想这样做编程方式在我的活动,这并没有改变任何东西。
I also tried doing it programmatically in my Activity, which didn't change anything.
addCodeView.findViewById(R.id.btnAddCode).setBackgroundTintList(
getResources().getColorStateList(R.color.accent));
为什么我的色彩忽略?
Why is my tint ignored?
编辑: 只是为了澄清,我确实是测试一个棒棒糖设备上。 其他部件(例如EditText上)是否正确,并自动着色。
Just to clarify, I am indeed testing on a Lollipop device. Other widgets (e.g. EditText) are correctly and automatically tinted.
推荐答案
看来,着色波纹绘制是没有意义的(和一个按钮的默认背景是纹波绘制)。
It seems that tinting a ripple drawable is meaningless (and the default background of a button is a ripple drawable).
事实上,在看该平台的默认按钮绘制后,我发现了正确的方式做到这一点:。你必须在你的主题定义的:
In fact, after looking at the platform's default button drawable, I found the "correct" way to do this:. You have to define this in your theme:
<item name="android:colorButtonNormal">@color/accent</item>
(当然这只是针对21±水平。)
(Of course this is only for level 21+.)
警告:因为这是在一个主题定义,该将使用给定的颜色为所有按钮(至少所有按钮中使用的主题活动)
Warning: since this is defined in a theme, this will use the given color for all the buttons (at least all of the buttons in activities using that theme.)
作为奖励,你也可以通过定义这种变化的波动颜色:
As a bonus, you can also change the ripple color by defining this:
<item name="android:colorControlHighlight">@color/accent_ripple</item>
这篇关于棒棒堂的backgroundTint有一个按钮没有任何影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!