Android getResources().getDrawable() deprecated API 22

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

    getDrawable(int drawable) is deprecated in API level 22. For reference see this link.

    Now to resolve this problem we have to pass a new constructer along with id like as :-

    getDrawable(int id, Resources.Theme theme)
    

    For Solutions Do like this:-

    In Java:-

    ContextCompat.getDrawable(getActivity(), R.drawable.name);   
    

    or

     imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));
    

    In Kotlin :-

    rel_week.background=ContextCompat.getDrawable(this.requireContext(), R.color.colorWhite)
    

    or

     rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)
    

    Hope this will help you.Thanks.

提交回复
热议问题