Lollipop 的 backgroundTint 对按钮没有影响 [英] Lollipop's backgroundTint has no effect on a Button
问题描述
我的 Activity 中有一个 Button,我希望它具有我的主题的强调色.不像我们必须像棒棒糖之前那样制作我自己的可绘制对象,自然我想使用新的 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.
我还尝试在我的 Activity 中以编程方式执行此操作,但没有任何改变.
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));
为什么我的色调会被忽略?
澄清一下,我确实在棒棒糖设备上进行测试.其他小部件(例如 EditText)已正确自动着色.
Just to clarify, I am indeed testing on a Lollipop device. Other widgets (e.g. EditText) are correctly and automatically tinted.
推荐答案
在 API 19 到 API 27 上测试
Tested on API 19 through API 27
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/retry"
android:textColor="@android:color/white"
app:backgroundTint="@android:color/holo_red_dark" />
产生输出为 -
这篇关于Lollipop 的 backgroundTint 对按钮没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!