Android getResources().getDrawable() 已弃用 API 22 [英] Android getResources().getDrawable() deprecated API 22
问题描述
使用新的 android API 22 getResources().getDrawable()
现在已弃用.现在最好的方法是只使用 getDrawable()
.
With new android API 22 getResources().getDrawable()
is now deprecated.
Now the best approach is to use only getDrawable()
.
发生了什么变化?
推荐答案
您有一些选项可以以正确(和面向未来)的方式处理这种弃用,具体取决于您正在加载的绘图类型:
You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:
A) 具有 主题属性
ContextCompat.getDrawable(getActivity(), R.drawable.name);
您将按照 Activity 主题的指示获得样式化的 Drawable.这可能正是您所需要的.
You'll obtain a styled Drawable as your Activity theme instructs. This is probably what you need.
B) drawables 没有主题属性
B) drawables without theme attributes
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
您将按照旧方式获得无样式的可绘制对象.请注意:ResourcesCompat.getDrawable()
未被弃用!
You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable()
is not deprecated!
EXTRA) 可绘制对象具有 来自另一个主题的主题属性
EXTRA) drawables with theme attributes from another theme
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
这篇关于Android getResources().getDrawable() 已弃用 API 22的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!