现在不推荐使用新的android API 22 getResources().getDrawable()
。现在最好的方法是仅使用getDrawable()
。
发生了什么变化?
您有一些选择以正确的方式(和未来的证明)来处理此弃用,这取决于您要加载的是哪种可绘制对象:
[A)可绘制对象with主题属性
ContextCompat.getDrawable(getActivity(), R.drawable.name);
您将按照Activity主题的指示获得样式化的Drawable。这可能就是您所需要的。
[B)可绘制对象无主题属性
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
您将以旧方式获得无样式的可绘制对象。请注意:ResourcesCompat.getDrawable()
已不推荐使用[[not!
EXTRA)
可绘制对象with来自[[another主题的主题属性ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
下面的代码行就足够了,ContextCompat.getDrawable将确保一切正常
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
//
} else {
//
}
ContextCompat.getDrawable(this, R.drawable.your_drawable_file)
。
对于解决方案,请像这样:-
在Java中:-
getDrawable(int id, Resources.Theme theme)
或
ContextCompat.getDrawable(getActivity(), R.drawable.name);
在Kotlin中:-
imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));
希望这对您有帮助。谢谢。
rel_week.background=ContextCompat.getDrawable(this.requireContext(), R.color.colorWhite)
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)
。
ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)
编辑
ResourcesCompat
现在也已弃用。但是您可以使用此:
[ContextCompat.getDrawable(this, R.drawable.your_drawable)
(此处为this
)]
有关更多详细信息,请单击此链接:ContextCompat
getResources().getDrawable()
(在API级别21中添加)getDrawable (int id, Resources.Theme theme)
这是一个如何验证更高版本的示例:
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));
}
这对我有用
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
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)));
public static List<ProductActivity> getCatalog(Resources res){
if(catalog == null) {
catalog.add(new Product("Dead or Alive", res
.getDrawable(R.drawable.product_salmon),
"Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));
catalog.add(new Product("Switch", res
.getDrawable(R.drawable.switchbook),
"Switch by Chip Heath and Dan Heath", 24.99));
catalog.add(new Product("Watchmen", res
.getDrawable(R.drawable.watchmen),
"Watchmen by Alan Moore and Dave Gibbons", 14.99));
}
}
marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));
context.getDrawable(R.drawable.your_drawable_name)