Java – Android support library: getdrawable independent vector or not

When using Android > = 5.0,

Drawable d = getResources().getDrawable(R.drawable.icon)

Parse the XML correctly and return a valid drawable However, when using the new vector drawable support library (version 23.4, gradle 2.1.2), this code crashes under Android 4

The solution is to use

Drawable d = VectorDrawableCompat.create(getResources(),R.drawable.icon,null);

However, if the resource is not a vector resource, it crashes:

So, what code must be used instead of the first line so that it applies to Android 4 and Android 6, as well as vector and non vector drawings In all cases, this line is used in Android 5.0 projects? The support library article does not mention how to perform this migration

Solution

I found a solution

You need to manually add support for vectordrawable

Try this in your activity:

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

You should check this blog entry for more information

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>