android getresources谁的方法,Android getResources() . getDrawable()不推荐使用API 22

回答(10)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

您有一些选项可以正确(和 future proof )方式处理此弃用,具体取决于您要加载的drawable类型:

A) 具有主题属性的drawables

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

您将在活动主题指示时获得样式化的Drawable . 这可能就是你所需要的 .

B) 没有主题属性的drawables

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

你将以旧的方式得到你的风格 . 请注意: ResourcesCompat.getDrawable() 是 not 已弃用!

EXTRA) 具有来自另一主题的主题属性的drawables

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

编辑:请参阅我关于该主题的博文,以获得更完整的解释

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

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

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

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

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

编辑

ResourcesCompat 现在也已弃用 . 但你可以用这个:

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

有关更多详细信息,请访问此链接:ContextCompat

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

getResources() . getDrawable()在API级别22中已弃用 . 现在我们必须添加主题:

这是一个例子:

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

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

您可以使用

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

这对我有用

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

只是我在数组中修复问题以加载listView的一个例子,希望它有所帮助 .

mItems = new ArrayList();

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

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

试试这个:

public static List 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));

}

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

en api等级14

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

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

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

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

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

context.getDrawable(R.drawable.your_drawable_name)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值