gpt4 book ai didi

android - 如何使用 AppCompat 库中的 VectorDrawableCompat?

转载 作者:行者123 更新时间:2023-11-29 19:42:30 26 4
gpt4 key购买 nike

我最初用 21 (Lollipop) 的 minsdkversion 编写了这个 SSCCE,它运行良好。 但我需要在 pre-lollipop 中使用 VectorDrawableCompat 在 SSCCE 中,XML 布局中有一个 ImageView,其中图像的来源是在 res/drawable/ 目录中定义的矢量图形。

最初,当 minsdkversion 在 list 中设置为 21 时,我使用 android:src="@drawable/vector_circle" 来设置矢量图形作为 ImageView 中图像的来源。这很好用。

然后我将 list 文件中的 minsdkversion 更改为 11。我还将 ImageViewandroid:src="@drawable/vector_circle" 属性更改为 app:srcCompat="@drawable/vector_circle" XML 布局。 但我正在接受

Unexpected namespace prefix "app" found for tag ImageView

app:srcCompat="@drawable/vector_circle" 行。我应该如何解决这个问题?

SSCCE:

res/drawable/vector_circle.xml:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="465dp"
android:height="420dp"
android:viewportHeight="600"
android:viewportWidth="600" >

<group>

<path android:strokeColor="#2196F3"
android:strokeWidth="2"
android:pathData="M150,10 L75,200 L225,200 Z" />

</group>

</vector>

activity_main:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/vector_circle" /><!-- app:srcCompat="@drawable/vector_circle" -->

</FrameLayout>

MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

编辑:

我将 res/layout/activity_main.xmlImageView 元素更改为

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/vector_circle"
tools:ignore="MissingPrefix" />

我还添加了 layout-v21/activity_main.xmlres/ 目录:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/vector_circle" /><!-- app:srcCompat="@drawable/vector_circle" -->

</FrameLayout>

并将 xmlns:tools="http://schemas.android.com/tools" 添加到 FrameLayout 元素。

预编译错误消失了。但是当我在 Lollipop 设备上运行该应用程序时,矢量图形绘制得非常好,但如果我在 Lollipop 之前的设备上运行该应用程序,应用程序不会失败,但根本不会绘制矢量图形。

最佳答案

要解决此问题,您只需使用可以处理 app:srcCompat 的小部件,即 AppCompatImageView 而不是 ImageView

关于android - 如何使用 AppCompat 库中的 VectorDrawableCompat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38409951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com