error when using app: srcCompat

0

I have a problem using srcCompat in the following code

 <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:background="@android:color/white"
        app:srcCompat="@drawable/ic_shopping_cart_black_24dp" />

the code with problems is

  

app: srcCompat="@ drawable / ic_shopping_cart_black_24dp"

The error I have is:

To use VectorDrawableCompat, you need to set 'android.defaultConfig.vectorDrawables.useSupportLibrary = true'.

The beginning of the xml

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"

I also have it activated in the build.gradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    
asked by Neil 12.11.2017 в 20:51
source

2 answers

1

Try adding this to your floating button:

tools:ignore="VectorDrawableCompat" 

If it does not work, after adding vectorDrawables.useSupportLibrary = true in your gradle, now in the class or classes you refer to your button add:

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

You must add this to each activity in which you want to use VectorDrawables on devices below Android 5.

    
answered by 12.11.2017 / 22:05
source
-1

I see that you have set up the support to use vector graphics, also if you use compileSdkVersion 26 , you must have a version of AS 2.0 +.

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
    ...
    ...
  }
}

It seems more like a cache problem, since enabling vector graphics support can be done within build.gradle . this to support the use of these graphics on all operating systems.

    
answered by 12.11.2017 в 22:36