问题描述
有人可以告诉我使用getDrawable()的正确方法吗,我记得上次使用一个参数就好了,但是现在我得到了这个方法已过时的错误,有人吗?波纹管是我要使用的代码段
Can anyone tell me the right way of using getDrawable() , I recall last time I used it was just fine with one parameter, but now I am getting this method is deprecated error, anyone? Bellow is the snippet I am trying to use
private void toggleUi() {
ImageView imageView = (ImageView) findViewById(R.id.silent_icon);
Drawable silentImage;
if(silent) {
silentImage = getResources().getDrawable(R.drawable.silent_on);
} else {
silentImage = getResources().getDrawable(R.drawable.silent_off);
}
imageView.setImageDrawable(silentImage);
}
如果有人可以帮助您,将非常高兴.
Will be glad if anyone can help thanks.
推荐答案
自API 22开始,您应该调用ContextCompat.getDrawable(context, R.drawable.***)
而不是getResources().getDrawable(R.drawable.***)
.
Since API 22 you should call ContextCompat.getDrawable(context, R.drawable.***)
instead of getResources().getDrawable(R.drawable.***)
.
您可以从以下位置查看 ContextCompat 的文档支持库以获取更多详细信息.
You can see the documentation of ContextCompat from the support library for more details.
这篇关于不推荐使用Android getDrawable()/如何使用Android getDrawable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!