作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 DrawableCompat 为 drawable 着色,如下所示,着色似乎不适用于 API 19。我使用的是支持 lib 版本 23.3.0
Drawable drawable = textView.getCompoundDrawables()[drawablePosition];
if (drawable != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.setTint(color);
} else {
DrawableCompat.setTint(DrawableCompat.wrap(drawable), color);
}
}
最佳答案
我遇到了同样的问题。我合并了 https://stackoverflow.com/a/30928051 中的帖子并使用 SupportLib 23.4.0 尝试了 API 17、19、21、22、23 和 N Preview 3 以找到解决方案。
即使提到,compat-class 将对 Lollipop 之前的设备使用过滤器(参见 https://stackoverflow.com/a/27812472/2170109),但它不起作用。
现在,我自己检查 API 并使用以下代码,该代码适用于所有经过测试的 API(适用于 17 及更高版本)。
// https://stackoverflow.com/a/30928051/2170109
Drawable drawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.vector));
image.setImageDrawable(drawable);
/*
* need to use the filter | https://stackoverflow.com/a/30880522/2170109
* (even if compat should use it for pre-API21-devices | https://stackoverflow.com/a/27812472/2170109)
*/
int color = ContextCompat.getColor(context, R.color.yourcolor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(drawable, color);
} else {
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
关于android - DrawableCompat setTint 不适用于 API 19,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36731919/
我是一名优秀的程序员,十分优秀!