Using Vector Drawable Compat

风格不统一 提交于 2019-12-04 07:56:29

问题


I am making an android app with a few fragments. In one of these fragments, I have a toolbar with a back arrow as an image button.
In the XML File, I have the "app:srcCompat" attribute, but I get an error when using this attribute saying this: "To use VectorDrawableCompat, you need to set 'android.defaultConfig.vectorDrawables.useSupportLibrary = true'


回答1:


In your module build.gradle file, you need to add this line:

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

        vectorDrawables.useSupportLibrary = true // This line here
    }
    ...
}

...



回答2:


add this line to your Gradle file under defaultConfig block:

vectorDrawables.useSupportLibrary = true

Also, you need to add this block of code in every activity or fragment where you're referencing drawables instead of images in srcCompat:

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }



回答3:


You have to add vectorDrawables.useSupportLibrary = true this line of code in you app level build.gradle inside the defaultConfig tag

defaultConfig {
        applicationId "your package Name"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "0.0.1"
        //This is the Main Line you have to add to avoid this warning.
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }



回答4:


You can use the following line

android:src="@drawable/edit"



回答5:


Add to your ImageButton:

tools:ignore="VectorDrawableCompat" 


来源:https://stackoverflow.com/questions/41226382/using-vector-drawable-compat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!