Android getResources()。getDrawable()已弃用API 22

问题描述 投票:666回答:12

现在不推荐使用新的android API 22 getResources().getDrawable()。现在最好的方法是仅使用getDrawable()

发生了什么变化?

android android-drawable android-resources android-5.1.1-lollipop
12个回答
946
投票

您有一些选择以正确的方式(和未来的证明)来处理此弃用,这取决于您要加载的是哪种可绘制对象:


[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);

0
投票
See docs

下面的代码行就足够了,ContextCompat.getDrawable将确保一切正常

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

0
投票
getDrawable(int drawable)在API级别22中已弃用。作为参考,请参见此

ContextCompat.getDrawable(this, R.drawable.your_drawable_file)

现在要解决此问题,我们必须将一个新的构造方法以及id传递给:-link

对于解决方案,请像这样:-

在Java中:-

getDrawable(int id, Resources.Theme theme)

ContextCompat.getDrawable(getActivity(), R.drawable.name);

在Kotlin中:-

imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme())); 希望这对您有帮助。谢谢。


-2
投票
rel_week.background=ContextCompat.getDrawable(this.requireContext(), R.color.colorWhite)

736
投票

您应该改用支持库中的以下代码:
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)


140
投票
带有ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)

编辑

ResourcesCompat现在也已弃用。但是您可以使用此:

[ContextCompat.getDrawable(this, R.drawable.your_drawable)(此处为this)]

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


28
投票
getResources().getDrawable()(在API级别21中添加)

这是一个示例:

getDrawable (int id, Resources.Theme theme)

这是一个如何验证更高版本的示例:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));

2
投票
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)); }

这对我有用


1
投票
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);

1
投票
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)));

0
投票
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)); } }

0
投票
marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));

context.getDrawable(R.drawable.your_drawable_name)

© www.soinside.com 2019 - 2024. All rights reserved.