Android getDrawable() Deprecated / How To use Android getDrawable()

南笙酒味 提交于 2019-12-23 22:16:04

问题


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.


回答1:


Since API 22 you should call ContextCompat.getDrawable(context, R.drawable.***) instead of getResources().getDrawable(R.drawable.***).

You can see the documentation of ContextCompat from the support library for more details.



来源:https://stackoverflow.com/questions/32765906/android-getdrawable-deprecated-how-to-use-android-getdrawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!