随着新的安卓API 22,getResources().getDrawable()
现在已经被弃用。
现在最好的方法是仅使用 getDrawable()
来获取Drawable对象。
发生了什么变化?
随着新的安卓API 22,getResources().getDrawable()
现在已经被弃用。
现在最好的方法是仅使用 getDrawable()
来获取Drawable对象。
发生了什么变化?
针对这种淘汰的情况,你有几种选项可以选择正确(并且具有未来证明)的方式进行处理,具体取决于您加载哪种类型的可绘制对象:
A) 具备主题属性的可绘制对象
ContextCompat.getDrawable(getActivity(), R.drawable.name);
根据您的Activity主题指示,您将获得一个已设置样式的Drawable。这很可能是您所需要的。
B) 没有主题属性的可绘制对象
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
你将以旧的方式获取未样式化的可绘制对象。请注意:ResourcesCompat.getDrawable()
没有过时!
额外) 从另一个主题获取带有主题属性的可绘制对象。
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
item.setEnabled(enabled);
Drawable originalIcon = ResourcesCompat.getDrawable(ctxt.getResources(), iconResId, null);
Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon);
item.setImageDrawable(icon);
}
并像这样调用它:Utility.setImageButtonEnabled(getContext(), false, back, R.drawable.arrow_left); - FractalBob您应使用支持库中的以下代码:
ContextCompat.getDrawable(context, R.drawable.***)
使用这种方法相当于调用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
从API 21开始,您应该使用getDrawable(int, Theme)
方法来替代getDrawable(int)
,因为它允许您获取与给定屏幕密度/主题相关联的特定资源ID的可绘制对象。调用已弃用的getDrawable(int)
方法等同于调用getDrawable(int, null)
。
Context
类的getDrawable(int id)
方法。这与getResources().getDrawable(id, getTheme())
相同,也使用了新的API。 - code monkeygetDrawable(int, Resources.Theme)
需要最低API级别为21。 - Prince将此行替换:
getResources().getDrawable(R.drawable.your_drawable)
用 ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)
代替。
编辑
ResourcesCompat
现在也已弃用。您可以使用以下方法:
ContextCompat.getDrawable(this, R.drawable.your_drawable)
(这里的this
是上下文)
有关更多详细信息,请访问此链接:ContextCompat
getResources().getDrawable()
在API级别22中已被弃用。现在我们必须添加主题:getDrawable(int id, Resources.Theme theme)(在API级别21中添加)
这是一个例子:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
这是一个验证后续版本的示例:
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));
}
Build.VERSION_CODES.LOLLIPOP
是 API 21,所以这个判断应该是 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
或者 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
?算了,从下面的话来看,“该方法在 API 21 中添加,但直到 API 22 才被弃用。:)” - Mark Cramer试一试
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
getDrawable(int drawable)在API级别22中已被弃用。 有关详细信息,请参阅此link。
现在要解决这个问题,我们必须传递一个新的构造函数和id,如下所示:
getDrawable(int id, Resources.Theme theme)
解决方案可以这样做:
在Java中:
ContextCompat.getDrawable(getActivity(), R.drawable.name);
或者
imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));
rel_week.background=ContextCompat.getDrawable(this, R.color.colorWhite)
or,
rel_week.background=ContextCompat.getDrawable(requireContext(), R.color.colorWhite)
)
或者
rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)
fun Context.getMyDrawable(id : Int) : Drawable?{
return ContextCompat.getDrawable(this, id)
}
然后使用类似如下的方式:
context.getMyDrawable(R.drawable.my_icon)
标签
。ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
对我来说是有效的
这只是一个关于如何解决数组问题以加载listView的示例,希望能有所帮助。
mItems = new ArrayList<ListViewItem>();
// Resources resources = getResources();
// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.az_lgo), getString(R.string.st_az), getString(R.string.all_nums)));
// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.ca_lgo), getString(R.string.st_ca), getString(R.string.all_nums)));
// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.co_lgo), getString(R.string.st_co), getString(R.string.all_nums)));
mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.az_lgo, null), getString(R.string.st_az), getString(R.string.all_nums)));
mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.ca_lgo, null), getString(R.string.st_ca), getString(R.string.all_nums)));
mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.co_lgo, null), getString(R.string.st_co), getString(R.string.all_nums)));
Resources
类的方法getDrawable(int id)
被弃用了。现在你应该使用带有新主题参数的方法getDrawable(int id,Resources.Theme theme)
。 - code monkeyResources#getDrawable(int)
和Resources#getColor(int)
都已被弃用。 - Alex Lockwood