使用矢量图兼容性库

49
我正在制作一个包含多个片段的Android应用程序。其中一个片段中,我有一个工具栏,其上有一个返回箭头,以图像按钮的形式呈现。
在XML文件中,我使用了"app:srcCompat"属性,但是当我使用此属性时,会出现错误,错误提示为:"要使用VectorDrawableCompat,您需要将'android.defaultConfig.vectorDrawables.useSupportLibrary = true'设置为true"。


3
好的,它已经告诉你解决方案了!如果要使用VectorDrawableCompat,你需要设置“android.defaultConfig.vectorDrawables.useSupportLibrary = true”。 - Phantômaxx
我有同样的问题,没有尝试建议的解决方案,但在旧设备和新设备上一切都正常工作。我想知道为什么这个警告会不断出现,如果它们似乎不影响任何事情。 - Gustavo
7
对于新手来说,不清楚这应该添加在哪里,更不用说如何添加了。 - John Perry
5个回答

114

在你的模块build.gradle文件中,你需要添加这行代码:

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

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

...

14
如果已经添加了但仍然出现“错误/警告”,该怎么办? - Muhammed Refaat
1
@MuhammedRefaat 请尝试重新同步您的项目。 - Rodin10

11

defaultConfig块下添加以下行到你的Gradle文件中:

vectorDrawables.useSupportLibrary = true

此外,在每个引用了srcCompat中的图片而不是drawable的活动或片段中,您需要添加以下代码块:

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

7

您需要在您的应用程序级别的build.gradle文件中的defaultConfig标签内添加vectorDrawables.useSupportLibrary = true这行代码。

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"
    }

0
您可以使用以下代码行。
android:src="@drawable/edit"

-18

在你的ImageButton中添加:

tools:ignore="VectorDrawableCompat" 

4
忽视问题并不是解决问题的方法。 - Ardy Febriansyah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接