android - 如何使用 Android 矢量绘图作为 Activity 的背景?

标签 android android-vectordrawable

我无法将矢量可绘制 xml 设置为 Android Activity 中的背景。

我尝试通过设置 android:background="@drawable/test_background" 将其设置为背景

  • 到我的 Activity 的协调器布局根。

  • 到协调器布局子约束布局

  • 发送至ImageView ConstraintLayout的 child 通过app:srcCompat"= @drawable/test_background"

以上方法均无效。我在设计预览中只能看到黑屏,并且我的应用程序因致命异常而崩溃:

android.view.InflateException: Binary XML file line #5: Error inflating class android.support.design.widget.CoordinatorLayout

这是我的布局概要:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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:background="@drawable/test_background"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:scrollbarStyle="outsideOverlay">

       <android.support.constraint.ConstraintLayout>
           <android.support.v7.widget.AppCompatImageView>
           </android.support.v7.widget.AppCompatImageView>

       </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

test_background 可绘制对象是从 png 图像创建的 xml 矢量资源,在 Inkscape 中转换为 svg,并通过 Inkscape 插件导出为 xml 资源。 png 图像包含一种模式,我从未真正将其上的任何内容转换为路径。所以它看起来像这样:

<vector xmlns:ns0="http://schemas.android.com/apk/res/android"
    ns0:name="svg869" 
    ns0:width="1382.2272dp" 
    ns0:height="2073.3408dp" 
    ns0:viewportWidth="666.666666667" 
    ns0:viewportHeight="1000.0"/>

但是,我确实确认该插件成功导出了 xml,因为我可以成功将 xml 背景设置为图标(例如作为 Logo /图标)。

我的问题是我无法将矢量设置为背景。

我需要在可绘制对象中设置任何属性才能将其用作 Activity 的背景吗?还是我做错了什么?

非常感谢您的帮助!

最佳答案

您需要将 SVG 转换为 Android 矢量资源。您可以使用 Android Studio 轻松完成此操作。 右键单击您的项目 > 新建 > 矢量资源。然后选择“本地文件”并输入文件位置/路径

enter image description here enter image description here

关于android - 如何使用 Android 矢量绘图作为 Activity 的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56112020/

相关文章:

android - 如何使 android Activity 适应所有尺寸的移动显示器?

java - Android volley future 请求超时

android - com.android.dex.DexException : Multiple dex files define Lcom/google/android/gms/analytics/internal/Command$1;

android - 如何根据 VectorDrawables 中的 Material 设计指南为应用程序图标创建渐变长阴影?

android - 如何使应用程序的矢量可绘制对象覆盖库的矢量可绘制对象?

android - 如何在Android中获取此音量设置?

JavaScript(使用innerHTML 添加到div) 如果更改此代码,我会提高cordova 应用程序的速度吗?

Android xml 包格式无法识别 aapt 属性

android studio SVG 文件导入出现错误@第 8 行 : Unsupported color format "rgba(0,0,0,0)"

带有 VectorDrawables srcCompat 的 Android Selector Drawable