问题描述
使用新的Android API 22 getResources()。getDrawable()
现在去precated。现在最好的办法是只使用 getDrawable()
。
With new android API 22 getResources().getDrawable()
is now deprecated.Now the best approach is to use only getDrawable()
.
什么样的变化?
推荐答案
您应使用以下code从支持库,而不是:
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);
}
由于API 21的,你应该使用 getDrawable(INT,主题)
方法,而不是 getDrawable(INT)
,因为它可以让你获取与给定的屏幕像素密度/主题特定资源ID相关联的绘制对象。调用pcated getDrawable(INT)
法德$ P $等同于调用 getDrawable(INT,空)
。
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. Calling the deprecated getDrawable(int)
method is equivalent to calling getDrawable(int, null)
.
这篇关于Android的getResources()。getDrawable()去precated API 22的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!