Android getResources().getDrawable() deprecated API 22

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

    You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:


    A) drawables with theme attributes

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

    You'll obtain a styled Drawable as your Activity theme instructs. This is probably what you need.


    B) drawables without theme attributes

    ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
    

    You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable() is not deprecated!


    EXTRA) drawables with theme attributes from another theme

    ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
    

提交回复
热议问题