【问题标题】:Using Vector Drawable Compat使用 Vector Drawable Compat
【发布时间】:2017-05-04 17:29:16
【问题描述】:

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

【问题讨论】:

  • 嗯......它已经告诉你解决方案了! To use VectorDrawableCompat, you need to set 'android.defaultConfig.vectorDrawables.useSupportLibrary = true'
  • 我有同样的问题,没有按照建议的解决方案,但一切都在新旧设备上运行。如果它们似乎没有影响任何东西,我想知道为什么这个警告会一直出现。
  • @ModularSynth 对于新手来说,不清楚应该在哪里添加,更不用说如何添加了。

标签: android android-vectordrawable


【解决方案1】:

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

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

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

...

【讨论】:

  • 如果已添加但仍收到“错误/警告”怎么办
  • @MuhammedRefaat 尝试再次同步您的项目
【解决方案2】:

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

vectorDrawables.useSupportLibrary = true

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

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

【讨论】:

    【解决方案3】:

    你必须在defaultConfig标签内的应用级build.gradle中添加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"
        }
    

    【讨论】:

      【解决方案4】:

      您可以使用以下行

      android:src="@drawable/edit"
      

      【讨论】:

        【解决方案5】:

        添加到您的 ImageButton:

        tools:ignore="VectorDrawableCompat" 
        

        【讨论】:

        • 忽略问题不是解决办法