所以我遇到了一个奇怪的问题......我制作了一些代码来为Drawable着色,并且它适用于Vector资产的所有Android版本,但不适用于常规PNG资产。代码如下:可绘制的着色代码适用于Vectors,但不适用于PNG
public class TintHelper {
private Context mContext;
public TintHelper(Context context) {
mContext = context;
}
public Drawable getTintedDrawableFromResource(int resourceID, ColorStateList colorStateList) {
Drawable original = AppCompatDrawableManager.get().getDrawable(mContext, resourceID);
return performTintOnDrawable(original, colorStateList);
}
private Drawable performTintOnDrawable(Drawable drawable, ColorStateList colorStateList) {
Drawable tinted = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(tinted, colorStateList);
return tinted;
}
}
当我指定一个矢量资源的资源ID,代码完美的作品,按下时图像着色,但是当我使用一个普通PNG,没有应用色调时,图标被按下。如果任何人有任何想法,为什么这不起作用,请发布一种可能支持这两种资产类型的替代方法。
提前致谢!
你正在使用什么版本的'appcompat-v7' /'support-v4'?最新的? 24.2.0? – pskink
@pskink 24.2.1。请参阅下面的答案以获取解决方案。 – privatestaticint
它只适用于24.2.0,我(双)检查,不需要自定义视图 – pskink