Invalid tag vector - appcompat-v7: 24.1.1

OK. I read most of the available answer to similar questions, but nothing fixed my problem.

gradle classpath 'com.android.tools.build:gradle:2.1.2'

android {
   defaultConfig {
      minSdkVersion 16
      targetSdkVersion 24
      vectorDrawables.useSupportLibrary = true
   }
}

dependencies {
    compile 'com.android.support:recyclerview-v7:24.1.1'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:design:24.1.1'
}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewItem"
    tools:ignore="MissingPrefix">

    ...

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:foregroundGravity="center_vertical|center|center_horizontal"
        app:srcCompat="@drawable/ic_car" />

    ...

</ScrollView>

vector ic_car.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24"
    android:viewportWidth="24">

    <path
        android:fillColor="#5c5c5c"
        android:pathData="M18.92 6.01C18.72 5.42 18.16 5 17.5 5h-11c-.66 0-1.21 .42 -1.42 1.01L3 12v8c0
.55 .45 1 1 1h1c.55 0 1-.45 1-1v-1h12v1c0 .55 .45 1 1 1h1c.55 0 1-.45
1-1v-8l-2.08-5.99zM6.5 16c-.83 0-1.5-.67-1.5-1.5S5.67 13 6.5 13s1.5 .67 1.5
1.5S7.33 16 6.5 16zm11 0c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5 .67 1.5
1.5-.67 1.5-1.5 1.5zM5 11l1.5-4.5h11L19 11H5z" />
    <path android:pathData="M0 0h24v24H0z" />
</vector>

my activity class expands AppCompatActivityand the tags android:srcchange to app:srcCompat, but still I see an error Invalid drawable tag vectoron minSdkVersionthat is set to 16.

+4
source share
4 answers

The problem is resolved itself after updating to Android studio 2.2

0
source

, , , srcCompat.
, , , drawable-21, Drawable , Drawables. , , - . Drawable.
, ImageView AppCompatImageView. , , , .
- , , , . - , , , leftImage ImageButton. 23.3, @Zeke. , 23.4, ,

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

, . . https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.s48hqaw1u, .

Drawable . - ;

Drawable icon = AppCompatDrawableManager.get().getDrawable(context, resVectorID);  
// Set it for example as a left image of button
button.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );

I have not tested this and have no idea if this causes memory problems or not. Will it be loved if someone can follow him.

+1
source

I like it:

public static final Drawable getMyDrawable(Context context, int id) {
     final int version = Build.VERSION.SDK_INT;
     if (version >= 21) {
         return ContextCompat.getDrawable(context, id);
     } else {
         return VectorDrawableCompat.create(context.getResources(), id,  context.getTheme());
     }
}
0
source

Source: https://habr.com/ru/post/1648865/


All Articles