Android getResources().getDrawable() deprecated API 22

后端 未结 14 2046
隐瞒了意图╮
隐瞒了意图╮ 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:05

    getResources().getDrawable() was deprecated in API level 22. Now we must add the theme:

    getDrawable (int id, Resources.Theme theme) (Added in API level 21)

    This is an example:

    myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
    

    This is an example how to validate for later versions:

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

提交回复
热议问题