我在使用新的AppCompat 23.3.x和可绘制对象时遇到了一些问题。首先,我不得不回去删除:
vectorDrawables.useSupportLibrary = true
因为AppCompat进行了还原,所以现在我的应用程序再次生成PNG。好的,然而,我正在以一种方式给一个按钮(drawableTop)添加着色,但这种方法在M之前的设备上完全无法工作。
以下是我使用的方法:
private void toggleState(boolean checked) {
Drawable[] drawables = getCompoundDrawables();
Drawable wrapDrawable = DrawableCompat.wrap(drawables[1]);
if (checked) {
DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),
R.color.colorPrimary));
setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
} else {
DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),
R.color.icon_light_enabled));
setTextColor(ContextCompat.getColor(getContext(), R.color.text_primary_light));
}
}
问题是,我有一个自定义的Button类,它可以被选中,如果选中了,drawableTop和文本的颜色与未选中时不同。
这个方法之前是有效的(使用appcompat 23.2.0),但现在(在Android M以下)已经无效了。你可以相信或者不相信,但当它执行setTint
方法时,图标将完全不可见。
要使其正常工作,我必须按照以下步骤操作:
DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),R.color.colorPrimary));
setCompoundDrawablesWithIntrinsicBounds(null, wrapDrawable, null, null);
每次我在它们上面涂色时,都必须再次调用
setCompoundDrawablesWithIntrinsicBounds
。这样做可以使一切重新运作。但是,我有点担心每次设置绘制内容的性能。基本上,我想知道是否有更好的方法或者我是否遗漏了什么东西。我知道一个
Button
具有setCompoundDrawableTintList
,这将非常棒,但其最小API级别为23级。
VectorDrawable
有什么关系? - pskinkContextCompat.getColor(getContext(),R.color.colorPrimary)
! - Justin Case