gpt4 book ai didi

android - DrawableCompat setTint 不适用于 API 19

转载 作者:IT老高 更新时间:2023-10-28 23:03:52 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com