DrawableCompat setTint不适用于API 19

时间:2016-04-20 00:47:46

标签: android android-support-library android-drawable

我正在使用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);
        }
    }

7 个答案:

答案 0 :(得分:26)

我遇到了同样的问题。我合并了https://stackoverflow.com/a/30928051中的帖子,并尝试使用SupportLib 23.4.0的API 17,19,21,22,23和N预览3来寻找解决方案。

即使提到,compat-class也会使用过滤器用于棒棒糖前设备(参见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);
    }

答案 1 :(得分:14)

使用AppCompat支持库(在24.1.1及更高版本上测试)在 API 15-25 上工作。

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;
}

答案 2 :(得分:1)

我认为在包装完drawable后,您需要在其上调用mutate()。请参阅:https://stackoverflow.com/a/30928051/3032209

答案 3 :(得分:0)

基于@localhost回答

Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.ic_rate));
DrawableCompat.setTint(d, Color.parseColor("#AAAAAA"));
l.setLogo(d);

我尝试使用API​​ 19> 25并且效果很好

答案 4 :(得分:0)

我使用了以下辅助方法来设置一个带有色调的drawable,它至少可以从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级。

答案 5 :(得分:0)

val textInput = EditText(context)

val drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
        drawable?.let { myDrawable ->
            DrawableCompat.setTint(myDrawable, ContextCompat.getColor(context, R.color.your_color))
            textInput.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, myDrawable, null)
        }

答案 6 :(得分:0)

在Honor 4X上,没有答案适合我。所以我进行了更多搜索,发现了这个:

您只需要让您的可绘制对象通过.invalidateSelf()进行更新

private void setTint(Drawable drawable, int color) {
    DrawableCompat.setTint(drawable, color);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        drawable.invalidateSelf();
}

Resourse: Tips for DrawableCompat.setTint() Under API 21