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

自API级别22起,Android的getResources().getDrawable()被弃用,推荐使用getDrawable(int id, Resources.Theme theme)。本文讨论了如何在不同情况下适配这一改变,包括使用ContextCompat和处理具有主题属性或无主题属性的绘图对象。" 127699688,366432,Boost C++ 文件系统操作详解,"['开发语言', 'C++', '文件操作', 'Boost库']
摘要由CSDN通过智能技术生成

本文翻译自:Android getResources().getDrawable() deprecated API 22

With new android API 22 getResources().getDrawable() is now deprecated. 使用新的android API 22,现在不建议使用getResources().getDrawable() Now the best approach is to use only getDrawable() . 现在最好的方法是仅使用getDrawable()

What changed? 发生了什么变化?


#1楼

参考:https://stackoom.com/question/1xqtf/Android-getResources-getDrawable-已弃用API


#2楼

Edit: see my blog post on the subject for a more complete explanation 编辑:有关此主题的更多信息 ,请参见我的博客文章


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);
}

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. 从API 21开始,应该使用getDrawable(int, Theme)方法代替getDrawable(int) ,因为它允许您针对给定的屏幕密度/主题获取与特定资源ID关联的可绘制对象。 Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null) . 调用不推荐使用的getDrawable(int)方法等效于调用getDrawable(int, null)


#3楼

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

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

EDIT 编辑

ResourcesCompat is also deprecated now. 现在不推荐使用ResourcesCompat But you can use this: 但是您可以使用以下命令:

ContextCompat.getDrawable(this, R.drawable.your_drawable) (Here this is the context) ContextCompat.getDrawable(this, R.drawable.your_drawable)下面this是上下文)

for more details follow this link: ContextCompat 有关更多详细信息,请单击此链接: ContextCompat


#4楼

You have some options to handle this deprecation the right (and future proof ) way, depending on which kind of drawable you are loading: 您有一些选择可以以正确的方式(以及将来的证明 )来处理这种弃用,这取决于您要加载的绘图类型:


A) drawables with theme attributes A) 具有主题属性的可绘制对象

ContextCompat.getDrawable(getActivity(), R.drawable.name);

You'll obtain a styled Drawable as your Activity theme instructs. 您将按照Activity主题的指示获得样式化的Drawable。 This is probably what you need. 这可能就是您所需要的。


B) drawables without theme attributes B) 没有主题属性的绘画

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

You'll get your unstyled drawable the old way. 您将以旧方式获得未样式化的可绘制对象。 Please note: ResourcesCompat.getDrawable() is not deprecated! 请注意: 推荐使用ResourcesCompat.getDrawable()


EXTRA) drawables with theme attributes from another theme 具有 其他主题的主题属性的EXTRA)可绘制对象

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);

#5楼

getResources().getDrawable() was deprecated in API level 22. Now we must add the theme: 在API级别22中不赞成使用getResources().getDrawable() 。现在,我们必须添加主题:

getDrawable (int id, Resources.Theme theme) (Added in API level 21) getDrawable(int id,Resources.Theme主题) (在API级别21中添加)

This is an example: 这是一个例子:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));

This is an example how to validate for later versions: 这是一个如何验证更高版本的示例:

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));
}

#6楼

Build.VERSION_CODES.LOLLIPOP should now be changed to BuildVersionCodes.Lollipop ie: Build.VERSION_CODES.LOLLIPOP现在应该更改为BuildVersionCodes.Lollipop,即:

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
    this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder, Context.Theme);
} else {
    this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值