getDrawable(int id) is deprecated. How can I set an image?

后端 未结 3 776
囚心锁ツ
囚心锁ツ 2021-01-16 07:19

Have a question! getDrawable() is deprecated in API 22. So, if I make an app with the min API 16, how can I set an image?

I saw that I can use getDrawable(int id, t

相关标签:
3条回答
  • 2021-01-16 07:57

    you can use

    yourImageView.setImageDrawable(ContextCompat.getDrawable(context, /*your drawable like R.drawable.drawableName*/));
    
    0 讨论(0)
  • 2021-01-16 08:06

    It seems that my project has a bug, but I can't identify it. If I try on other projects, it works with any of the answers above.

    0 讨论(0)
  • 2021-01-16 08:10

    You can try this way,

     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return context.getResources().getDrawable(resource);
    } else {
        return context.getResources().getDrawable(resource, null);
    }
    

    may helps you

    0 讨论(0)
提交回复
热议问题