java - Android:context.getDrawable() 的替代方案

标签 java android eclipse android-drawable

我在我的项目中使用过这样的 context.getDrawable():

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

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

最佳答案

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

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.

正如 this answer 中指出的那样更好的解决方案是使用 ContextCompat : ContextCompat.getDrawable(context, R.drawable.***)

关于java - Android:context.getDrawable() 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27796246/

相关文章:

java - 为命令行 Java 应用程序创建可运行的 JAR 文件

java - eclipse 找不到包和类

Java:集合

java - Android 应用程序 intellij Layout_marginBottom 正在添加到顶部

java - 从方法中获取恒定的错误值

java - 使用triangle_strip模式处理形状的纹理

java - Java中的线程挂起时如何停止?

java - 未处理的事件循环异常 Eclipse

android - 声学指纹识别背后的原理是什么?

android - 是否可以根据国家/地区提供多个 APK 支持?