Java Android:替代 context.getDrawable()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27796246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Alternative for context.getDrawable()
提问by Bram
I have used context.getDrawable()
like this in my project:
我context.getDrawable()
在我的项目中使用过这样的:
Drawable greenProgressbar = context.getDrawable(R.drawable.custom_progressbargreen);
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()
.
但是 Eclipse 给了我一个错误,它需要一个Minimum API level of 21
. 这意味着在快速谷歌搜索后,我的 APP 只能在Android 5.0
. 由于并非所有设备都使用此版本的 android,因此我想为context.getDrawable()
.
采纳答案by user2417480
The previously accepted method has been deprecated, according to the SDK 22 documentation:
根据 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.
在 android.os.Build.VERSION_CODES#JELLY_BEAN 之前,当此处传递的资源 ID 是另一个 Drawable 资源的别名时,此函数将无法正确检索最终配置密度。这意味着如果别名资源的密度配置与实际资源不同,则返回的 Drawable 的密度将不正确,从而导致不好的缩放。
As pointed out in this answerbetter solution would be to use ContextCompat:
ContextCompat.getDrawable(context, R.drawable.***)
正如在这个答案中指出的那样,更好的解决方案是使用ContextCompat:
ContextCompat.getDrawable(context, R.drawable.***)
回答by Bram
I had a similar problem before. Have you tried to do it like this?
我之前也遇到过类似的问题。你试过这样做吗?
Drawable greenProgressbar = context.getResources().getDrawable(R.drawable.custom_progressbargreen);
回答by glm9637
Try adding a getResources()
after the context, so this:
尝试getResources()
在上下文之后添加一个,这样:
Drawable greenProgressbar = context.getResources().getDrawable(R.drawable.custom_progressbargreen);
should work.
应该管用。
回答by Umanda
I had a same situation which I wanted to reference getDrawable() method which is now deprecated.
我有同样的情况,我想引用现在已弃用的 getDrawable() 方法。
what I used,
我用过的
myButton.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_btn_off));
Hope this will help you
希望能帮到你
回答by Nando
Try this:
尝试这个:
AppCompatResources.getDrawable(context, R.drawable.*)
回答by Maulik Baraiya
You should use " getDrawable(id, this.getTheme()) ". This method is not deprecated till now.
你应该使用“ getDrawable(id, this.getTheme()) ”。直到现在,这种方法还没有被弃用。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setBackground(getResources().getDrawable(R.drawable.radioline,this.getTheme()));
} else {
view.setBackground(getResources().getDrawable(R.drawable.radioline));
}
回答by Kaylen Travis Pillay
I agree using ContextCompact.getDrawable(Context context, int resID). It worked for me and my app targets API 19.
我同意使用 ContextCompact.getDrawable(Context context, int resID)。它对我有用,我的应用程序针对 API 19。
回答by Lukas Mohs
You can also set the resource directly without working with the drawable (Kotlin):
你也可以直接设置资源而不使用 drawable (Kotlin):
btn.setImageResource(R.drawable.ic_XXX)