建议更改build.gradle文件后,后台drawable会导致膨胀异常

问题描述 投票:0回答:2

在我的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文件之前,这些背景drawable正常工作。

笔记:

  • 我想我可以添加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>
android xml android-layout warnings vector-graphics
2个回答
0
投票

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

dependencies {

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

0
投票

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

如果你仔细阅读,这里暗示(通过'省略',真的): 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

© www.soinside.com 2019 - 2024. All rights reserved.