建议更改 build.gradle 文件后,背景可绘制对象会导致膨胀异常
Background drawables cause inflate exception after suggested change to build.gradle file
在我的 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>
将这些依赖项添加到您的 应用级别 gradle 文件:
dependencies {
implementation 'com.mikhaellopez:circularimageview:3.2.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
原来你不能使用支持库在背景上使用矢量绘图。
如果你仔细阅读,它在这里暗示了这一点('omittance',真的):
Developer.Andriod - Vector drawables backward compatibility solution
Related:
Is that right to make android.view.InflateException by using Vector drawables in android:background: line in a ImageView
在我的 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>
将这些依赖项添加到您的 应用级别 gradle 文件:
dependencies {
implementation 'com.mikhaellopez:circularimageview:3.2.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
原来你不能使用支持库在背景上使用矢量绘图。
如果你仔细阅读,它在这里暗示了这一点('omittance',真的):
Developer.Andriod - Vector drawables backward compatibility solution
Related:
Is that right to make android.view.InflateException by using Vector drawables in android:background: line in a ImageView