使用Vector Drawable Compat

问题描述 投票:21回答:4

我正在制作一个带有几个片段的Android应用程序。在其中一个片段中,我有一个带有后箭头的工具栏作为图像按钮。 在XML文件中,我有“app:srcCompat”属性,但是在使用这个属性时我得到一个错误:“要使用VectorDrawableCompat,你需要设置'android.defaultConfig.vectorDrawables.useSupportLibrary = true'

android android-vectordrawable
4个回答
56
投票

在您的模块build.gradle文件中,您需要添加以下行:

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

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

...

7
投票

将此行添加到defaultConfig块下的Gradle文件中:

vectorDrawables.useSupportLibrary = true

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

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

0
投票

您可以使用以下行

android:src="@drawable/edit"

-6
投票

添加到您的ImageButton:

tools:ignore="VectorDrawableCompat" 
© www.soinside.com 2019 - 2024. All rights reserved.