Android DrawableCompat setTint不适用于API 19
我使用DrawableCompat对drawable进行着色,如下所示,着色似乎不适用于API 19。我使用的是支持库23.3.0版Android DrawableCompat setTint不适用于API 19,android,android-support-library,android-drawable,Android,Android Support Library,Android Drawable,我使用DrawableCompat对drawable进行着色,如下所示,着色似乎不适用于API 19。我使用的是支持库23.3.0版 Drawable drawable = textView.getCompoundDrawables()[drawablePosition]; if (drawable != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { draw
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);
}
}
我认为在包装您的drawable之后,您需要对其调用
mutate()
。看到这个:我也有同样的问题。我结合了中的帖子,并尝试了API 17、19、21、22、23和N Preview 3以及SupportLib 23.4.0来找到解决方案
即使提到compat类将为棒棒糖前的设备使用过滤器(请参阅),它也不起作用
现在,我自己检查API并使用以下代码,这些代码正在所有测试过的API(17及以上)上运行
使用AppCompat支持库(在24.1.1及以上版本中测试)在API 15-25上工作 根据答案
我在API 19>25上试过,效果很好我使用了下面的助手方法来设置一个带有色调的可绘制图形,它至少可以从API级别16一直工作到x。颜色是主题颜色,因此通过属性解析
public static Drawable getTintedDrawable(int drawableResourceId, int colorAttribute) {
YourApplication = YourApplication.getInstance();
Drawable drawable = ContextCompat.getDrawable(application, drawableResourceId);
drawable = DrawableCompat.wrap(drawable);
int tintColor = ContextCompat.getColor(application, application.getThemedResourceId(colorAttribute));
DrawableCompat.setTint(drawable, tintColor);
return drawable;
}
没有drawable.wrap,它可以在更高版本上工作,但不能在api级别16上工作。
val textInput=EditText(上下文)
val drawable=ContextCompat.getDrawable(context,R.drawable.your_drawable)
可绘制?.let{myDrawable->
setTint(myDrawable,ContextCompat.getColor(context,R.color.your_color))
textInput.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,myDrawable,null)
}
在荣誉4X上没有答案对我有效。所以我搜索了更多,发现了这个:
只需通过.invalidateSelf()
private void setTint(可绘制,内部颜色){
可拉伸相容性设置色调(可拉伸,颜色);
if(Build.VERSION.SDK\u INT
我尝试使用支持库24.2.1,但不幸的是,它在API 19上不起作用。我继续使用@hardysim的解决方案。在API 19/21/25上使用支持库25.1.1对我来说,只有
mutate()
就足够了。@Eselfar这是可行的,但是请注意,您必须添加行d=DrawableCompat。包装(d);这将包装drawable以对其进行变异,而设置tintIt仅在添加DrawableCompat.wrap(drawable)
时起作用。请允许我问一下.mutate()
在上述情况下有什么用?最后是一些对我有用的棒棒糖。非常感谢您的分享。它不适合我的棒棒糖(API 21)。我不得不将最低版本更改为API22(Build.version\u code.LOLLIPOP\u MR1)。将hardysim和@Eselfar的答案结合在一起对我的情况有效。欢迎使用stackoverflow。你能在你的回答中加上一点解释吗?我认为这个条件也没有必要!
public static Drawable getTintedDrawable(@NonNull final Context context,
@DrawableRes int drawableRes, @ColorRes int colorRes) {
Drawable d = ContextCompat.getDrawable(context, drawableRes);
d = DrawableCompat.wrap(d);
DrawableCompat.setTint(d.mutate(), ContextCompat.getColor(context, colorRes));
return d;
}
Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.ic_rate));
DrawableCompat.setTint(d, Color.parseColor("#AAAAAA"));
l.setLogo(d);
public static Drawable getTintedDrawable(int drawableResourceId, int colorAttribute) {
YourApplication = YourApplication.getInstance();
Drawable drawable = ContextCompat.getDrawable(application, drawableResourceId);
drawable = DrawableCompat.wrap(drawable);
int tintColor = ContextCompat.getColor(application, application.getThemedResourceId(colorAttribute));
DrawableCompat.setTint(drawable, tintColor);
return drawable;
}
private void setTint(Drawable drawable, int color) {
DrawableCompat.setTint(drawable, color);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
drawable.invalidateSelf();
}