Android:替代context.getDrawable() [英] Android: Alternative for context.getDrawable()

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

问题描述

我在我的项目中使用了 context.getDrawable()

I have used context.getDrawable() like this in my project:

Drawable greenProgressbar = context.getDrawable(R.drawable.custom_progressbargreen);

但是Eclipse给我一个错误,它需要一个最低API级别21 。这意味着在快速搜索Google后,我的APP只能在 Android 5.0 上使用。由于并不是所有的设备都使用这个版本的Android,我想要有一个替代方案 context.getDrawable()

But Eclipse is giving me an error that it needs a Minimum API level of 21. This would mean after a quick google search my APP will only be usable on Android 5.0. Since not all devices are using this version of android I would like to have an alternative for context.getDrawable().

推荐答案

根据SDK 22文档,以前接受的方法已被弃用:

The previously accepted method has been deprecated, according to the SDK 22 documentation:


对于android.os.Build.VERSION_CODES#JELLY_BEAN,当这里传递的资源ID是另一个Drawable资源的别名时,此函数将无法正确检索最终的配置密度。这意味着如果别名资源的密度配置与实际资源不同,则返回的Drawable的密度将不正确,导致错误的缩放。

Prior to android.os.Build.VERSION_CODES#JELLY_BEAN, this function would not correctly retrieve the final configuration density when the resource ID passed here is an alias to another Drawable resource. This means that if the density configuration of the alias resource is different than the actual resource, the density of the returned Drawable would be incorrect, resulting in bad scaling.

这个答案所指出的,更好的解决方案是使用 ContextCompat
ContextCompat.getDrawable(context,R .drawable。***)

As pointed out in this answer better solution would be to use ContextCompat: ContextCompat.getDrawable(context, R.drawable.***)

这篇关于Android:替代context.getDrawable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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