Xamarin

getResources().getDrawable() dep

2018-10-08  本文已影响8人  Xamarin信仰中心

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

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

2. 为什么这个 API deprecated

API 21 (Android 5.0 Lollipop) 引入了一些新的主题属性 (new theme attributes),例如 android:colorAccent, 它修改了包含对那些新主题属性值的引用的 drawable 的外观. AppCompat 库为您处理 Lollipop 前后绘制样式。

如果使用废弃的 API getDrawable() 方法来获取具有主题属性的可绘制资源,您将获得部分样式的 drawablelogcat 警告。您可以在 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:

@Deprecated
@Nullable
public 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;
}

3. 解决方案

使用以下的 API

ContextCompat.getDrawable(context, R.drawable.***)

这个 API 等同于:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}

API 21 开始,我们应该使用 getDrawable(int, Theme) 方法而不是 getDrawable(int),因为它允许我们获取与给定屏幕密度/主题的特定资源 ID 关联的可绘制对象

直接调用 deprecated 的 getDrawable(int)方法等同于调用 getDrawable(int, null)

4. 参考链接

上一篇下一篇

猜你喜欢

热点阅读