【问题标题】:Background drawables cause inflate exception after suggested change to build.gradle file建议更改 build.gradle 文件后,背景可绘制对象导致膨胀异常
【发布时间】:2019-04-08 17:48:37
【问题描述】:

在我的 Android Studio 项目中,我收到关于我的 XML 布局中任何 app:srcCompat="@drawable/..." 属性的警告:

<ImageView
    android:id="@+id/leftNav"
    android:layout_width="wrap_content"
    android:layout_height="46dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:adjustViewBounds="true"
    android:clickable="true"
    android:focusable="true"
    android:onClick="leftNav_Click"
    android:scaleType="fitCenter"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_chevron_left_black_24dp"<!--warning-->
    tools:ignore="ContentDescription"
    />

警告:
Inspection info:To use VectorDrawableCompat, you need to make two modifications to your project. First, set android.defaultConfig.vectorDrawables.useSupportLibrary = true in your build.gradle file, and second, use app:srcCompat instead of android:src to refer to vector drawables.

所以我采取了这些步骤,如build.gradle 应用文件中所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.testappbehaviourchart"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        android.defaultConfig.vectorDrawables.useSupportLibrary = true //added to resolve VectorDrawableCompat issue
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha03'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.viewpager:viewpager:1.0.0'
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:support-compat:28.0.0"
    implementation 'com.google.android.material:material:1.0.0'

}

这似乎抑制了警告,但是,我现在在 inflate 上收到一个新的致命运行时异常:

`E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testappbehaviourchart, PID: 21284
    android.view.InflateException: Binary XML file line #165: Error inflating class <unknown>`

我已将其范围缩小到任何 android:background="@drawable/..." 属性 就像这里从TableRow 内部的TableLayout 内部显示的那样:

<View
    android:id="@+id/view_m1"
    android:layout_width="match_parent"
    android:layout_height="82dp"
    android:background="@drawable/ic_simplebox"
    android:minWidth="82dp"
    tools:layout_editor_absoluteX="105dp"
    tools:layout_editor_absoluteY="101dp"
    /> 

在我将该行添加到 gradle 文件之前,这些背景可绘制对象工作正常。

注意事项:

  • 我想我可以添加 tools:ignore="VectorDrawingCompat" 以忽略第一个警告,但我不确定会带来什么影响。
  • 我需要将minSdkVersion 保持为 16。
  • 这是@drawable/ic_simplebox XML:
<vector android:height="24dp" android:viewportHeight="100"
    android:viewportWidth="100" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#E0171C20" android:name="Shape 1 copy" android:pathData="m20 100v-5h60v5h-60z"/>
    <path android:fillColor="#E0171C20" android:name="Shape 1 copy 2" android:pathData="m0 20h5v60h-5v-60z"/>
</vector>
  • 这是@drawable/ic_chevron_left_black_24dp XML:
<vector android:height="24dp" android:tint="#667573"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>
</vector>

【问题讨论】:

  • 我不这么认为,这将在棒棒糖版本之前起作用。你能分享drawable文件吗?
  • 嗨@Goutham我添加了上面的xml。当您说“我不这么认为”时,您指的是警告还是例外?
  • 正如您所提到的,您的最低 sdk 版本是 16,但 app:srcCompat 将从 Android 支持库 23.3.0 开始工作,因此您必须正确处理棒棒糖下方的手机。 “android:src”可以帮助我猜。
  • 包含 android:background="@drawable/ic_simplebox" 的 XML 布局(导致充气异常)在我的 PagerAdapter 类上充气是否重要?我尝试将 this 添加到 PagerAdapter 但没有奏效。

标签: android xml android-layout warnings vector-graphics


【解决方案1】:

将这些依赖项添加到您的 应用级 gradle 文件中:

dependencies {

    implementation 'com.mikhaellopez:circularimageview:3.2.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}

【讨论】:

    【解决方案2】:

    事实证明,您不能使用支持库在背景上使用 Vector Drawable。

    如果您仔细阅读,它会在此处暗示这一点(实际上是“省略”):
    Developer.Andriod - Vector drawables backward compatibility solution

    相关:
    Is that right to make android.view.InflateException by using Vector drawables in android:background: line in a ImageView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 2017-07-05
      • 2016-11-27
      相关资源
      最近更新 更多