819

With new android API 22 getResources().getDrawable() is now deprecated. Now the best approach is to use only getDrawable().

What changed?

4

16 回答 16

1241

您有一些选项可以以正确(和未来证明)的方式处理这种弃用,具体取决于您正在加载的绘图类型:


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);
于 2015-03-19T14:13:35.820 回答
752

编辑:查看我关于该主题的博客文章以获得更完整的解释


您应该改用支持库中的以下代码:

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).

于 2015-03-13T20:32:35.980 回答
145

替换这一行: getResources().getDrawable(R.drawable.your_drawable)

ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)

编辑

ResourcesCompat现在也被弃用了。但是你可以使用这个:

ContextCompat.getDrawable(this, R.drawable.your_drawable)(这里this是上下文)

有关更多详细信息,请点击此链接:ContextCompat

于 2015-03-18T08:32:09.150 回答
29

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));
}
于 2015-09-29T21:25:46.647 回答
8

在 Kotlin 中,您可以使用扩展

fun Context.getMyDrawable(id : Int) : Drawable?{

    return  ContextCompat.getDrawable(this, id)
}

然后像

context.getMyDrawable(R.drawable.my_icon)
于 2019-11-12T04:42:37.147 回答
7

尝试这个

ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
于 2021-05-03T09:52:36.980 回答
5

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)

希望这会帮助你。谢谢。

于 2019-10-11T05:21:22.690 回答
3

您可以使用

ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);

这对我有用

于 2016-12-26T04:36:32.037 回答
1

如果您的目标是 SDK > 21(棒棒糖或 5.0),请使用

context.getDrawable(R.drawable.your_drawable_name)

查看文档

于 2018-09-24T20:21:03.627 回答
1

尝试这个:

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));
    }
}
于 2017-02-09T14:47:42.233 回答
1

只是我如何在数组中修复问题以加载 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)));
于 2016-01-12T17:40:50.140 回答
0

现在你需要像这样实现

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
        //
    } else {
        //
    }

只需一行代码就足够了,一切都将由 ContextCompat.getDrawable 处理

ContextCompat.getDrawable(this, R.drawable.your_drawable_file)
于 2019-03-01T04:20:10.837 回答
0

zh API 级别 14

marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));
于 2018-01-20T16:46:16.213 回答
0

对于一些即使在应用了这个线程的建议(我曾经是这样的人)之后仍然要解决这个问题的人来说,在你的 Application 类上添加这一行,onCreate() 方法

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)

正如此处此处所建议的那样,有时需要从资源中访问向量,尤其是在处理菜单项等时

于 2019-10-30T08:24:34.843 回答
0

如果您需要从其他面向 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);
于 2021-02-28T05:41:53.263 回答
-2

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);
}
于 2015-12-02T01:06:11.707 回答