问题
I am super excited about the new possibility to set vector drawables to layouts using the app:srcCompat="@drawable/icon"
attribute introduced in support-library 23.2.0.
But I wonder how I can fetch one of this drawables programmatically or set is as background.
I thought about something like:
ContextCompat.getDrawable(context, R.drawable.icon)
Is this even possible?
回答1:
Okay, I managed it myself. Thanks for @Budius for pointing me in the right direction.
The Answers lays in VectorDrawable#create()
Resources resources = context.getResources(Resources, int, Theme);
Theme theme = context.getTheme();
Drawable drawable = VectorDrawableCompat.create(resources, R.drawable.drawable, theme);
More input: How to use vector drawables in Android API lower 21?
回答2:
You can even use,
imageView.setImageResource(id);
where id is your R.drawable.img
来源:https://stackoverflow.com/questions/35915974/23-2-0-set-vector-drawable-as-background-in-4-x