Android getResources().getDrawable() deprecated API 22

后端 未结 14 1844
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 07:14

With new android API 22 getResources().getDrawable() is now deprecated. Now the best approach is to use only getDrawable().

What changed? <

相关标签:
14条回答
  • 2020-11-22 08:09

    Edit: see my blog post on the subject for a more complete explanation


    You should use the following code from the support library instead:

    ContextCompat.getDrawable(context, R.drawable.***)
    

    Using this method is equivalent to calling:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return resources.getDrawable(id, context.getTheme());
    } else {
        return resources.getDrawable(id);
    }
    

    As of API 21, you should use the getDrawable(int, Theme) method instead of getDrawable(int), as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null).

    0 讨论(0)
  • 2020-11-22 08:09

    Replace this line : getResources().getDrawable(R.drawable.your_drawable)

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

    EDIT

    ResourcesCompat is also deprecated now. But you can use this:

    ContextCompat.getDrawable(this, R.drawable.your_drawable) (Here this is the context)

    for more details follow this link: ContextCompat

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