我使用下面的方法在android.support.design 23.0.1中正确地为复合可拉伸材料着色。现在他们发布了23.1.0,它不再适用于API LVL16,我所有的可绘制文件都是黑色的。
有人有什么建议吗?
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);
}
谢谢。
最佳答案
上周我也遇到了同样的问题,结果在appcompattextview v23.1.0中,复合可拉伸材料会自动着色。
下面是我找到的解决方案,并对我为什么这样做做做了更多的解释。它不是很干净,但至少它能让你着色你的复合拉丝!
解决方案
将此代码放入帮助程序类或自定义文本视图/按钮中:
/**
* 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();
}
}
然后可以对扩展appcompattextview或appcompatbutton的类的每个构造函数调用
removeDefaultTinting(this);
。例如:public MyCustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
removeDefaultTinting(this);
}
这样,使用v23.0.1的代码就应该在v23.1.0上工作。
我不满意使用反射来更改appcompat库中的属性,但这是我发现的在v23.1.0中对复合可绘制文件使用着色的唯一方法。希望有人能找到更好的解决方案,或者复合可拉伸着色将添加到AppCompat公共方法中。
更新
我发现了另一个更简单的解决方案:只有在使用xml设置复合drawables时,才会出现此错误。不要用XML设置它们,然后在代码中设置它们,这样就可以工作了。错误代码在构造函数中,在调用后设置drawables不受影响。
解释
在AppCompatTextView构造函数中,已初始化文本帮助程序:
mTextHelper.loadFromAttributes(attrs, defStyleAttr);
mTextHelper.applyCompoundDrawablesTints();
在texthhelper
loadFromAttributes
函数中,为每个可绘制的复合图创建一个着色列表。如您所见,mDrawableXXXTint.mHasTintList
始终设置为true。mDrawableXXXTint.mTintList
是将应用的淡色,仅从appcompat的硬编码值获取。对于您的自定义可提取项,它将始终为空。所以你最终得到的是一个“色调列表”为空的色调。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();
}
}
因此,如果将色调应用于复合可绘制,然后调用超级方法(如
view.setCompoundDrawablesRelativeWithIntrinsicBounds
),则文本帮助器会将其空色调应用于可绘制,删除您所做的所有操作…最后,下面是应用色调的函数:
final void applyCompoundDrawableTint(Drawable drawable, TintInfo info) {
if (drawable != null && info != null) {
TintManager.tintDrawable(drawable, info, mView.getDrawableState());
}
}
参数中的
TintInfo
是texthelper类的mDrawableXXXTint
属性。如您所见,如果为空,则不应用色调。将所有可绘制的着色属性设置为空将阻止AppCompat应用其着色,并使您能够对可绘制的内容执行任何所需的操作。我没有找到一个干净的方法来阻止这种行为或让它应用我想要的颜色。所有属性都是私有的,没有getter。