Android getResources().getDrawable()已弃用API 22

Blo*_*ard 672 android android-resources android-drawable android-5.1.1-lollipop

新的Android API 22 getResources().getDrawable()现已弃用.现在最好的方法是只使用getDrawable().

改变了什么?

ara*_*aks 962

您可以选择以正确(以及将来证明)的方式处理此弃用,具体取决于您要加载的drawable类型:


A)具有主题属性的drawable

ContextCompat.getDrawable(getActivity(), R.drawable.name);
Run Code Online (Sandbox Code Playgroud)

您将在活动主题指示时获得样式化的Drawable.这可能就是你所需要的.


B)没有主题属性的drawables

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
Run Code Online (Sandbox Code Playgroud)

你将以旧的方式得到你的风格.请注意:ResourcesCompat.getDrawable()不是过时!


EXTRA)具有来自另一主题的主题属性的drawable

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
Run Code Online (Sandbox Code Playgroud)


Ale*_*ood 737

编辑:请参阅关于该主题的博客文章,以获得更完整的解释


您应该使用支持库中的以下代码:

ContextCompat.getDrawable(context, R.drawable.***)
Run Code Online (Sandbox Code Playgroud)

使用此方法相当于调用:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}
Run Code Online (Sandbox Code Playgroud)

从API 21开始,您应该使用该getDrawable(int, Theme)方法而不是getDrawable(int),因为它允许您获取与给定屏幕密度/主题的特定资源ID关联的可绘制对象.调用已弃用的getDrawable(int)方法等同于调用getDrawable(int, null).

  • 根据文档,使用`getDrawable(int,Resources.Theme)`需要min API level 21. (9认同)
  • 我认为OP也引用了`Context`类的`getDrawable(int id)`方法.这与`getResources().getDrawable(id,getTheme());`相同,并且还使用新的API. (8认同)
  • 这只会增加我的代码行.可恶! (5认同)

vin*_*091 140

替换此行: getResources().getDrawable(R.drawable.your_drawable)

ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)

编辑

ResourcesCompat现在也弃用了.但你可以用这个:

ContextCompat.getDrawable(this, R.drawable.your_drawable)(这this是上下文)

有关更多详细信息,请访问以下链接:ContextCompat

  • 不赞成使用ResourcesCompat.getDrawable()!请阅读下面的答案! (6认同)
  • 多数民众赞成现在:http://plus.google.com/+BenjaminWeiss/posts/M1dYFaobyBM (2认同)
  • 它没有在[link](https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html)中说不推荐使用`ResourcesCompat`.它应该工作正常. (2认同)

Jor*_*sys 28

getResources().getDrawable() 在API级别22中已弃用.现在我们必须添加主题:

getDrawable(int id,Resources.Theme theme) (在API级别21中添加)

这是一个例子:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
Run Code Online (Sandbox Code Playgroud)

这是如何验证更高版本的示例:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
     myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
   } else { 
     myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage));
}
Run Code Online (Sandbox Code Playgroud)


小智 13

尝试这个

ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
Run Code Online (Sandbox Code Playgroud)


Rah*_*aha 10

getDrawable(int drawable)在 API 级别 22 中已弃用。有关参考,请参阅此链接

现在要解决这个问题,我们必须传递一个新的构造函数以及 id,如下所示:-

getDrawable(int id, Resources.Theme theme)
Run Code Online (Sandbox Code Playgroud)

对于解决方案请这样做:-

在爪哇中:-

ContextCompat.getDrawable(getActivity(), R.drawable.name);   
Run Code Online (Sandbox Code Playgroud)

或者

 imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));
Run Code Online (Sandbox Code Playgroud)

在科特林中:-

rel_week.background=ContextCompat.getDrawable(this, R.color.colorWhite)
 or,
rel_week.background=ContextCompat.getDrawable(requireContext(), R.color.colorWhite)
)
Run Code Online (Sandbox Code Playgroud)

或者

 rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)
Run Code Online (Sandbox Code Playgroud)

希望这会对您有所帮助。谢谢。


Dee*_*yan 6

在 Kotlin 中,您可以使用扩展

fun Context.getMyDrawable(id : Int) : Drawable?{

    return  ContextCompat.getDrawable(this, id)
}
Run Code Online (Sandbox Code Playgroud)

然后使用像

context.getMyDrawable(R.drawable.my_icon)
Run Code Online (Sandbox Code Playgroud)