本文翻译自:Android getResources().getDrawable() deprecated API 22
With new android API 22 getResources().getDrawable()
is now deprecated. 使用新的android API 22,现在不建议使用getResources().getDrawable()
。 Now the best approach is to use only getDrawable()
. 现在最好的方法是仅使用getDrawable()
。
What changed? 发生了什么变化?
#1楼
参考:https://stackoom.com/question/1xqtf/Android-getResources-getDrawable-已弃用API
#2楼
Edit: see my blog post on the subject for a more complete explanation 编辑:有关此主题的更多信息 ,请参见我的博客文章
You should use the following code from the support library instead: 您应该改用支持库中的以下代码:
ContextCompat.getDrawable(context, R.drawable.***)
Using this method is equivalent to calling: 使用此方法等效于调用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
As of API 21, you should use the getDrawable(int, Theme)
method instead of getDrawable(int)
, as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. 从API 21开始,应该使用getDrawable(int, Theme)
方法代替getDrawable(int)
,因为它允许您针对给定的屏幕密度/主题获取与特定资源ID关联的可绘制对象。 Calling the deprecated getDrawable(int)
method is equivalent to calling getDrawable(int, null)
. 调用不推荐使用的getDrawable(int)
方法等效于调用getDrawable(int, null)
。
#3楼
Replace this line : getResources().getDrawable(R.drawable.your_drawable)
替换此行: getResources().getDrawable(R.drawable.your_drawable)
with ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)
与ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)
EDIT 编辑
ResourcesCompat
is also deprecated now. 现在不推荐使用ResourcesCompat
。 But you can use this: 但是您可以使用以下命令:
ContextCompat.getDrawable(this, R.drawable.your_drawable)
(Here this
is the context) ContextCompat.getDrawable(this, R.drawable.your_drawable)
下面this
是上下文)
for more details follow this link: ContextCompat 有关更多详细信息,请单击此链接: ContextCompat
#4楼
You have some options to handle this deprecation the right (and future proof ) way, depending on which kind of drawable you are loading: 您有一些选择可以以正确的方式(以及将来的证明 )来处理这种弃用,这取决于您要加载的绘图类型:
A) drawables with theme attributes A) 具有主题属性的可绘制对象
ContextCompat.getDrawable(getActivity(), R.drawable.name);
You'll obtain a styled Drawable as your Activity theme instructs. 您将按照Activity主题的指示获得样式化的Drawable。 This is probably what you need. 这可能就是您所需要的。
B) drawables without theme attributes B) 没有主题属性的绘画
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
You'll get your unstyled drawable the old way. 您将以旧方式获得未样式化的可绘制对象。 Please note: ResourcesCompat.getDrawable()
is not deprecated! 请注意: 不推荐使用ResourcesCompat.getDrawable()
!
EXTRA) drawables with theme attributes from another theme 具有 其他主题的主题属性的EXTRA)可绘制对象
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
#5楼
getResources().getDrawable()
was deprecated in API level 22. Now we must add the theme: 在API级别22中不赞成使用getResources().getDrawable()
。现在,我们必须添加主题:
getDrawable (int id, Resources.Theme theme) (Added in API level 21) getDrawable(int id,Resources.Theme主题) (在API级别21中添加)
This is an example: 这是一个例子:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
This is an example how to validate for later versions: 这是一个如何验证更高版本的示例:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
} else {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage));
}
#6楼
Build.VERSION_CODES.LOLLIPOP should now be changed to BuildVersionCodes.Lollipop ie: Build.VERSION_CODES.LOLLIPOP现在应该更改为BuildVersionCodes.Lollipop,即:
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder, Context.Theme);
} else {
this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder);
}