getResources().getDrawable() deprecated API 22
stackoverflow
: https://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22
You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:
1. API 详解
A) drawables with theme attributes
You'll obtain a styled Drawable as your Activity theme instructs.
This is probably what you need.
B) drawables without theme attributes
You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable()
is not deprecated!
EXTRA) drawables with theme attributes from another theme
2. 为什么这个 API deprecated
API 21 (Android 5.0 Lollipop) 引入了一些新的主题属性 (new theme attributes),例如 android:colorAccent
, 它修改了包含对那些新主题属性值的引用的 drawable
的外观. AppCompat
库为您处理 Lollipop
前后绘制样式。
如果使用废弃的 API getDrawable()
方法来获取具有主题属性的可绘制资源,您将获得部分样式的 drawable
和 logcat
警告。您可以在 API 22
中看到这个 android.content.res.Resources
源代码:
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:
3. 解决方案
使用以下的 API
这个 API 等同于:
从 API 21
开始,我们应该使用 getDrawable(int, Theme)
方法而不是 getDrawable(int)
,因为它允许我们获取与给定屏幕密度/主题的特定资源 ID
关联的可绘制对象
直接调用 deprecated 的 getDrawable(int)
方法等同于调用 getDrawable(int, null)
4. 参考链接
- ContextCompat Reference
- ResourcesCompat Reference
- Benjamin Weiss g+ post