我已设置视图的 backgroundTint ,如下所示,但它对其颜色没有影响。在xml设计视图中,它看起来很好但不在设备上。我的设备上有棒棒糖的版本。
compileSdkVersion和targetSdkVersion是21。
<View
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/circle_appointment_statuses"
android:backgroundTint="@color/pending" />
答案 0 :(得分:1)
坏消息
在Lollipop 5.0(API级别21)中为Button的背景着色毫无意义。
好消息
Lollipop 5.1(API级别22)似乎通过更改btn_mtrl_default_shape.xml(以及其他文件)修复了此问题:https://android.googlesource.com/platform/frameworks/base/+/6dfa60f33ca6018959ebff1efde82db7d2aed1e3%5E!/#F0
好消息
新支持库(版本22.1+)adds backward-compatible tinting support包含许多组件,包括AppCompatButton!
不幸的是,android:backgroundTint属性仍然不起作用(也许我做错了) - 所以你必须使用setSupportBackgroundTintList()在代码中设置ColorStateList。很高兴看到未来支持android:backgroundTint。
更多详情请访问此处。Lollipop's backgroundTint has no effect on a Button
答案 1 :(得分:0)
要在棒棒糖上的视图(API 21)上绘制可绘制背景,请使用setColorFilter(int color, PorterDuff.Mode mode)
。
layout.getBackground().setColorFilter(ContextCompat.getColor(context, R.color.color), PorterDuff.Mode.SCR_ATOP)
以下方法不适用于棒棒糖上的背景可绘制对象。
background.setTint(int color)
DrawableCompat.setTint(Drawable drawable, int color)