对于新的android API 22,getResources().getDrawable()现在已被弃用。现在最好的方法是只使用getDrawable()。
什么改变了?
对于新的android API 22,getResources().getDrawable()现在已被弃用。现在最好的方法是只使用getDrawable()。
什么改变了?
当前回答
您可以使用
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
这是我的工作
其他回答
在Kotlin,您可以使用扩展
fun Context.getMyDrawable(id : Int) : Drawable?{
return ContextCompat.getDrawable(this, id)
}
然后使用like
context.getMyDrawable(R.drawable.my_icon)
如果您的目标SDK>21(棒棒糖或5.0),请使用
context.getDrawable(R.drawable.your_drawable_name)
参见文档
这只是我如何在数组中修复问题以加载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)));
试试看:
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));
}
}
en-api级别14
marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));