Android AppCompat 23.1.0 Tint Compound Drawable

时间:2015-10-19 18:18:19

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

我使用下面的方法用android.support.design 23.0.1正确地着色复合drawables。现在他们发布了23.1.0,它在api LVL16上不再起作用,我所有的drawable都是黑色的。

有人有建议吗?

  private void setCompoundColor(TextView view) {
    Drawable drawable = view.getCompoundDrawables()[0];
    Drawable wrap = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(wrap, ContextCompat.getColor(this, R.color.primaryLighter2));
    DrawableCompat.setTintMode(wrap, PorterDuff.Mode.SRC_IN);
    wrap = wrap.mutate();
    view.setCompoundDrawablesRelativeWithIntrinsicBounds(wrap, null, null, null);
  }

感谢。

2 个答案:

答案 0 :(得分:9)

我上周遇到了同样的问题,结果在AppCompatTextView v23.1.0中,复合drawables会自动着色。

这是我找到的解决方案,更详细说明了我为什么在下面这样做。它不是很干净,但至少它可以让你染色你的化合物drawables!

<强>解

将此代码放在帮助器类或自定义TextView / Button中:

/**
 * The app compat text view automatically sets the compound drawable tints for a static array of drawables ids.
 * If the drawable id is not in the list, the lib apply a null tint, removing the custom tint set before.
 * There is no way to change this (private attributes/classes, only set in the constructor...)
 *
 * @param object the object on which to disable default tinting.
 */
public static void removeDefaultTinting(Object object) {
    try {
        // Get the text helper field.
        Field mTextHelperField = object.getClass().getSuperclass().getDeclaredField("mTextHelper");
        mTextHelperField.setAccessible(true);
        // Get the text helper object instance.
        final Object mTextHelper = mTextHelperField.get(object);
        if (mTextHelper != null) {
            // Apply tint to all private attributes. See AppCompat source code for usage of theses attributes.
            setObjectFieldToNull(mTextHelper, "mDrawableStartTint");
            setObjectFieldToNull(mTextHelper, "mDrawableEndTint");
            setObjectFieldToNull(mTextHelper, "mDrawableLeftTint");
            setObjectFieldToNull(mTextHelper, "mDrawableTopTint");
            setObjectFieldToNull(mTextHelper, "mDrawableRightTint");
            setObjectFieldToNull(mTextHelper, "mDrawableBottomTint");
        }
    } catch (NoSuchFieldException e) {
        // If it doesn't work, we can do nothing else. The icons will be white, we will see it.
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // If it doesn't work, we can do nothing else. The icons will be white, we will see it.
        e.printStackTrace();
    }
}

/**
 * Set the field of an object to null.
 *
 * @param object    the TextHelper object (class is not accessible...).
 * @param fieldName the name of the tint field.
 */
private static void setObjectFieldToNull(Object object, String fieldName) {
    try {
        Field tintField;
        // Try to get field from class or super class (depends on the implementation).
        try {
            tintField = object.getClass().getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            tintField = object.getClass().getSuperclass().getDeclaredField(fieldName);
        }
        tintField.setAccessible(true);
        tintField.set(object, null);

    } catch (NoSuchFieldException e) {
        // If it doesn't work, we can do nothing else. The icons will be white, we will see it.
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // If it doesn't work, we can do nothing else. The icons will be white, we will see it.
        e.printStackTrace();
    }
}

然后,您可以在类的每个构造函数上调用removeDefaultTinting(this);,扩展AppCompatTextView或AppCompatButton。例如:

public MyCustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    removeDefaultTinting(this);
}

有了这个,使用v23.0.1的代码应该适用于v23.1.0。

我不满意使用反射来更改AppCompat lib中的属性,但这是我发现在v23.1.0上使用复合drawable的着色的唯一方法。希望有人会找到更好的解决方案,或者将复合可绘制着色添加到AppCompat公共方法中。

<强>更新

我发现了另一个更简单的解决方案:只有在使用xml设置复合drawable时才会出现此错误。不要在xml中设置它们,然后在代码中设置它们就可以了。构造函数中的错误代码,在调用后设置drawables不会受到影响。

<强>吃茶

在AppCompatTextView构造函数中,初始化文本助手:

mTextHelper.loadFromAttributes(attrs, defStyleAttr);
mTextHelper.applyCompoundDrawablesTints();

在TextHelper loadFromAttributes函数中,为每个复合drawable创建一个色调列表。如您所见,mDrawableXXXTint.mHasTintList始终设置为true。 mDrawableXXXTint.mTintList是将应用的色调颜色,仅来自AppCompat的硬编码值。对于您的自定义drawable,它将始终为null。所以你最终会得到一个带有空“色调列表”的色调。

TypedArray a = context.obtainStyledAttributes(attrs, VIEW_ATTRS, defStyleAttr, 0);
    final int ap = a.getResourceId(0, -1);

    // Now read the compound drawable and grab any tints
    if (a.hasValue(1)) {
        mDrawableLeftTint = new TintInfo();
        mDrawableLeftTint.mHasTintList = true;
        mDrawableLeftTint.mTintList = tintManager.getTintList(a.getResourceId(1, 0));
    }
    if (a.hasValue(2)) {
        mDrawableTopTint = new TintInfo();
        mDrawableTopTint.mHasTintList = true;
        mDrawableTopTint.mTintList = tintManager.getTintList(a.getResourceId(2, 0));
    }

...

问题是这个色调是在构造函数中应用的,每次设置或更改drawable:

 @Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (mBackgroundTintHelper != null) {
        mBackgroundTintHelper.applySupportBackgroundTint();
    }
    if (mTextHelper != null) {
        mTextHelper.applyCompoundDrawablesTints();
    }
}

因此,如果您对复合drawable应用色调,然后调用超级方法(如view.setCompoundDrawablesRelativeWithIntrinsicBounds),文本助手会将其空色调应用于您的drawable,删除您已完成的所有内容...... < / p>

最后,这是应用色调的函数:

final void applyCompoundDrawableTint(Drawable drawable, TintInfo info) {
    if (drawable != null && info != null) {
        TintManager.tintDrawable(drawable, info, mView.getDrawableState());
    }
}

参数中的TintInfo是texthelper类的mDrawableXXXTint属性。如您所见,如果它为null,则不应用任何色调。将所有可绘制色调属性设置为null可防止AppCompat应用其色调,并使您可以使用drawables执行所需的视频。

我没有找到一种干净的方法来阻止这种行为或让它应用我想要的色调。所有属性都是私有的,没有getter。

答案 1 :(得分:0)

您可以尝试这样的事情

ContextCompat.getDrawable(context, R.drawable.cool_icon)?.apply {
    setTint(ContextCompat.getColor(context, R.color.red))
}