你有一些选项来处理这种弃用正确的(和未来的证明)的方式,这取决于你正在加载哪种类型的可绘制:
A)具有主题属性的drawables
ContextCompat.getDrawable(getActivity(), R.drawable.name);
您将获得一个风格化的Drawable作为您的活动主题指示。
这可能是你需要的。
B)没有主题属性的drawables
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
你会得到你的未画的drawable旧的方式。
请注意:ResourcesCompat.getDrawable()不推荐使用!
EXTRA)drawables与来自另一个主题的主题属性
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
说明:
API 21(Android 5.0 Lollipop)引入了一些新的主题属性,如android:colorAccent,它们修改可绘制对象的外观,这些对象引用了这些新的主题属性值。
AppCompat库处理前和后Lollipop可绘制样式为您。
如果您使用已弃用的getDrawable()方法来获取具有主题属性的可绘制资源,那么您将获得部分风格的drawable和logcat警告。
你可以在API 22中看到这个android.content.res.Resources源代码:
@Deprecated
@Nullable
public Drawable getDrawable(int id) throws NotFoundException {
final Drawable d = getDrawable(id, null);
if (d != null && d.canApplyTheme()) {
Log.w(TAG, "Drawable " + getResourceName(id) + " has unresolved theme "
+ "attributes! Consider using Resources.getDrawable(int, Theme) or "
+ "Context.getDrawable(int).", new RuntimeException());
}
return d;
}