以下解决方法摘自 stackoverflow , 点击下方链接可跳转

Android getResources().getDrawable() deprecated API 22

No.1

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.

EXTRA) drawables with theme attributes from another theme

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

Explanation:

API 21 (Android 5.0 Lollipop) introduced some new theme attributes such as android:colorAccent that modify the appearance of drawables that hold references to those new theme attributes values.The AppCompat library handles pre and post-Lollipop drawable styling for you.

If you do use the deprecated getDrawable() method to obtain a drawable resource with theme attributes, you will get a partially-styled drawable and a logcat warning.You can see this in API 22 android.content.res.Resources source code:

@Deprecated@Nullablepublic Drawable getDrawable(int id) throws NotFoundException {    final Drawable d = getDrawable(id, null);    if (d != null && d.canApplyTheme()) {        Log.w(TAG, "Drawable " + getResourceName(id) + " has unresolved theme "                + "attributes! Consider using Resources.getDrawable(int, Theme) or "                + "Context.getDrawable(int).", new RuntimeException());    }    return d;}

No.2

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).

更多相关文章

  1. android中ImageView、ImageButton、Button之间的区别
  2. android 按钮的点击缩放
  3. android 获取wifi 信号质量
  4. Android用开发者模式进行调试时出现的问题及解决方案
  5. 【Android】隐藏底部虚拟按键,亲测可用
  6. TextView添加Onclick点击无效没反应解决办法
  7. Android中点击事件之KeyListener实现步骤
  8. 关于android中的Toast的用法
  9. Android使用Fragment嵌套Fragment的模式实现界面滑动

随机推荐

  1. Android(安卓)VideoView播放视频
  2. Android(安卓)Camera ShutterSound custo
  3. android 事件
  4. 判断Android的网络状态
  5. Android软键盘用法实例
  6. android获取解析GPS数据
  7. Android自定义view二Banner
  8. [Android] Android(安卓)Sutdio on Surfa
  9. android Volley 使用
  10. Android获取手机信息