With new android API 22 getResources().getDrawable()
is now deprecated.
Now the best approach is to use only getDrawable()
.
What changed?
With new android API 22 getResources().getDrawable()
is now deprecated.
Now the best approach is to use only getDrawable()
.
What changed?
您有一些选项可以以正确(和未来证明)的方式处理这种弃用,具体取决于您正在加载的绘图类型:
A)具有主题属性的可绘制对象
ContextCompat.getDrawable(getActivity(), R.drawable.name);
您将根据 Activity 主题的指示获得一个样式化的 Drawable。这可能是您需要的。
B)没有主题属性的drawable
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
您将以旧方式获得无样式的可绘制对象。请注意:ResourcesCompat.getDrawable()
不推荐使用!
EXTRA)具有来自另一个主题的主题属性的可绘制对象
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
您应该改用支持库中的以下代码:
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)
.
替换这一行:
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 主题) (在 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));
}
在 Kotlin 中,您可以使用扩展
fun Context.getMyDrawable(id : Int) : Drawable?{
return ContextCompat.getDrawable(this, id)
}
然后像
context.getMyDrawable(R.drawable.my_icon)
尝试这个
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
getDrawable(int drawable)在 API 级别 22 中已弃用。有关参考,请参阅此链接。
现在要解决这个问题,我们必须传递一个新的构造函数以及 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.requireContext(), R.color.colorWhite)
或者
rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)
希望这会帮助你。谢谢。
您可以使用
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
这对我有用
尝试这个:
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));
}
}
只是我如何在数组中修复问题以加载 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)));
现在你需要像这样实现
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
//
} else {
//
}
只需一行代码就足够了,一切都将由 ContextCompat.getDrawable 处理
ContextCompat.getDrawable(this, R.drawable.your_drawable_file)
zh 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);
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);
}