通过在ImageView中的android:background:行中使用Vector drawables来使android.view.InflateException正确吗 [英] Is that right to make android.view.InflateException by using Vector drawables in android:background: line in a ImageView
问题描述
对于某些特定设备,我在我的应用中正面临android.view.InflateException
,而在其他电话中,它运行正常.但是我不知道为什么.许多其他人说,可绘制矢量图像会出现此问题.我在android:background= ""
行中使用矢量图像.是使这个InflateException
.
I am facing android.view.InflateException
in my app for some particular devices, in others phone it works fine. But i don't know why. Many others says that this problem occurs for drawable vector image. I am using vector image in android:background= ""
line. is that make this InflateException
.
<RelativeLayout
android:id="@+id/nameLayoutMain"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:background="@drawable/shadow_164949">
<ImageView
android:layout_width="80dp"
android:layout_height="400dp"
android:background="@drawable/draw_phone"
android:id="@+id/im1"
android:layout_alignParentTop="true" />
<ImageView
android:layout_width="80dp"
android:layout_height="50dp"
app:srcCompat="@drawable/ic_phone"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/linearLayoutName"
android:layout_toStartOf="@+id/linearLayoutName" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayoutName"
android:gravity="center_vertical"
android:layout_alignBottom="@+id/im1"
android:layout_toRightOf="@+id/im1"
android:layout_toEndOf="@+id/im1">
<TextView
android:id="@+id/txtPhoneShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_phone_number"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/txtPhone"
android:layout_alignStart="@+id/txtPhone" />
<TextView
android:id="@+id/txtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="01612548792"
android:textColor="@color/toolbar1"
android:textSize="20sp"
android:textStyle="bold"
android:layout_below="@+id/txtPhoneShow"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp" />
</RelativeLayout>
</RelativeLayout>
推荐答案
确保已在app/build.gradle
中的defaultConfig
内添加了vectorDrawables.useSupportLibrary = true
.
Make sure you have added vectorDrawables.useSupportLibrary = true
inside defaultConfig
in app/build.gradle
.
喜欢
defaultConfig {
vectorDrawables.useSupportLibrary = true
....
}
编辑1
为了在API中支持小于21的vector
,还应在setContentView
之前的Activity的onCreate
方法中添加它.
EDIT 1
In order to support vector
in API less then 21. Also add this in onCreate
method of Activity before setContentView
.
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
这篇关于通过在ImageView中的android:background:行中使用Vector drawables来使android.view.InflateException正确吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!