【问题标题】:Why DrawableCompat.setTint() method is not woking correct?为什么 DrawableCompat.setTint() 方法不正确?
【发布时间】:2016-07-05 11:07:56
【问题描述】:

我有一个问题。 为什么当我使用

Drawable drawable = getResources().getDrawable(R.drawable.some_drawable);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.white));

不工作,当我使用时

Drawable drawable = getResources().getDrawable(R.drawable.some_drawable);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), R.color.white);

工作了吗?

在文档中是这样的

public static void setTint(@NonNull Drawable drawable, @ColorInt int tint){}

所以意味着我需要提供资源,而不是 int。

谢谢。

编辑:

如果我提供颜色资源,我看看我的函数是否用@ColorInt(..., @ColorInt int color) 进行了注释,是否可以正常工作。越来越混乱。如果不是,则忽略资源颜色。

【问题讨论】:

  • tint: Color to use for tinting this drawable,所以它需要一个 32 位长的 int 颜色,而不是资源 id
  • 顺便说一句,@ColorInt 是颜色值,而 @ColorRes 是资源 ID。

标签: android drawable android-appcompat tint


【解决方案1】:

您必须在使用之前解析颜色。 R.color.white 只是一个指向 R 文件中 id 的指针。

通过调用getResources().getColor(R.color.white),您可以解析颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2019-04-03
    相关资源
    最近更新 更多