Invalid drawable tag vector

后端 未结 14 958
小蘑菇
小蘑菇 2020-12-28 12:34

Im trying to use vector drawables on pre lollipop devices. I did all as instructed here but i still get this crash.

build.gradle:

相关标签:
14条回答
  • 2020-12-28 12:38

    The answers given here are ignoring a situation when you wish to add a drawable to a textview because it gives the same error. in my case I had

    <TextView .... android:drawableLeft="some_vectore_drawable" />
    

    I could not find how to solve this so I removed that line from the xml code and put it in my java code in this manner

    Drawable somevectordrable = AppCompatDrawableManager.get().getDrawable(context, R.drawable.somevectordrawable);
    mytextview.setCompoundDrawableWithIntrinsicBounds(somevectordrable, null, null, null);
    

    Clarification for the code,

    1. Get the vector drawable from the drawables folder using AppCompatDrawableManager

    2. Set the drawable we just got as the left drawable on our textview

    0 讨论(0)
  • 2020-12-28 12:39

    The problem was that my activity wasn't extending AppCompatActivity but regular Activity.

    This is not specified in any documentation/example for support vector drawables

    0 讨论(0)
  • 2020-12-28 12:41

    Got this problem too when loading vectors from a selector on pre-lollipop devices:

    Use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) in your onCreate method:

    Sets whether vector drawables on older platforms (< API 21) can be used within android.graphics.drawable.DrawableContainer resources. When enabled, AppCompat can intercept some drawable inflation from the framework, which enables implicit inflation of vector drawables within android.graphics.drawable.DrawableContainer resources.

    protected final void onCreate(Bundle savedInstanceState) {
             AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
             super.onCreate(savedInstanceState);
             ...
    
    0 讨论(0)
  • 2020-12-28 12:44

    If you see yourself using <android.support.v7.widget.AppCompatImageView when loading a vector drawable it might be a better idea to extend AppCompatActivity instead of Activity and go back to using regular <ImageView...app:srcCompat="@drawable/...

    0 讨论(0)
  • 2020-12-28 12:45

    This is support-v4, appcompat-v7 library v23.2.0 bug it appears in API 19. @tim provide link to this bug issue.

    You can upgrade to new library version 23.2.1 and bug is fixed now.

    0 讨论(0)
  • 2020-12-28 12:45

    If you're having this issue with drawableLeft/drawableStart, etc. there is an easy solution, if you're using databinding.

    Instead of:

    android:drawableLeft="@drawable/somevector" ❌
    

    Do:

    android:drawableLeft="@{@drawable/somevector}" ✅
    

    This works because databinding will generate code at compile time that retrieves the drawable in a compatible way.

    0 讨论(0)
提交回复
热议问题