随着新的安卓API 22,getResources().getDrawable()
现在已经被弃用。
现在最好的方法是仅使用 getDrawable()
来获取Drawable对象。
发生了什么变化?
随着新的安卓API 22,getResources().getDrawable()
现在已经被弃用。
现在最好的方法是仅使用 getDrawable()
来获取Drawable对象。
发生了什么变化?
试试这个:
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));
}
}
英文 API 等级 14
marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));
如果您需要从针对SDK 23及以上的其他应用程序获取可绘制对象
PackageManager manager = getApplicationContext().getPackageManager();
Resources resources = null;
try {
resources = manager.getResourcesForApplication("com.anyapp");
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
assert resources != null;
Drawable notiIcon = ResourcesCompat.getDrawable(resources, current.iconId/* drawable resource id */, null);
现在你需要实现像这样
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
//
} else {
//
}
只需要以下一行代码,ContextCompat.getDrawable会处理所有事情
ContextCompat.getDrawable(this, R.drawable.your_drawable_file)
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);
}
Resources
类的方法getDrawable(int id)
被弃用了。现在你应该使用带有新主题参数的方法getDrawable(int id,Resources.Theme theme)
。 - code monkeyResources#getDrawable(int)
和Resources#getColor(int)
都已被弃用。 - Alex Lockwood