如何膨胀包含矢量可绘制图标的布局?
How to inflate layout containing vector drawable icon?
我想膨胀包含 ImageView
的 layout
和 vector xml
背景可绘制对象。
val view = LayoutInflater.from(parent.context).inflate(R.layout.my_layout, parent, false)
但这会导致异常:
android.view.InflateException: Binary XML file line #72: Binary XML file line #72: Error inflating class ImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
...
我尝试将可绘制图标移到 res/drawable-v21/
中,但这似乎是 LayoutInflater
本身的问题。
我试过使用 androidx.appcompat.widget.AppCompatImageView
而不是 ImageView
但这没有用。
图像视图:
<ImageView
android:id="@+id/layout_ico"
android:layout_width="@dimen/t_icon_size"
android:layout_height="@dimen/t_icon_size"
android:background="@drawable/vector_ico"/>
矢量图标(xml 的示例 - 不能 post 精确图标):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="vectorPath"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
您在使用 appcompatImageView 时是否更新了 gradle 文件?
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
并且在您的布局中
<androidx.appcompat.widget.AppCompatImageView
...
app:srcCompat="@drawable/something" />
除了在您的 gradle 文件中包含以下行:
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
您还需要在应用程序中添加以下行 class:
class App : Application() {
override fun onCreate() {
super.onCreate()
...
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
}
我想膨胀包含 ImageView
的 layout
和 vector xml
背景可绘制对象。
val view = LayoutInflater.from(parent.context).inflate(R.layout.my_layout, parent, false)
但这会导致异常:
android.view.InflateException: Binary XML file line #72: Binary XML file line #72: Error inflating class ImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
...
我尝试将可绘制图标移到 res/drawable-v21/
中,但这似乎是 LayoutInflater
本身的问题。
我试过使用 androidx.appcompat.widget.AppCompatImageView
而不是 ImageView
但这没有用。
图像视图:
<ImageView
android:id="@+id/layout_ico"
android:layout_width="@dimen/t_icon_size"
android:layout_height="@dimen/t_icon_size"
android:background="@drawable/vector_ico"/>
矢量图标(xml 的示例 - 不能 post 精确图标):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="vectorPath"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
您在使用 appcompatImageView 时是否更新了 gradle 文件?
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
并且在您的布局中
<androidx.appcompat.widget.AppCompatImageView
...
app:srcCompat="@drawable/something" />
除了在您的 gradle 文件中包含以下行:
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
您还需要在应用程序中添加以下行 class:
class App : Application() {
override fun onCreate() {
super.onCreate()
...
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
}