Android的getResources()。getDrawable()去precated API 22 [英] Android getResources().getDrawable() deprecated API 22

查看:368
本文介绍了Android的getResources()。getDrawable()去precated API 22的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的Andr​​oid 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆