如何将Android矢量可绘制对象用作活动的背景?

时间:2019-05-13 12:07:33

标签: android android-vectordrawable

我无法在Android活动中将矢量可绘制xml设置为背景。

我尝试通过设置android:background="@drawable/test_background"

将其设置为背景
  • 到我活动的协调器布局根。

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

  • 通过ImageViewConstraintLayout的{​​{1}}的{​​{1}}子级

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

  

android.view.InflateException:二进制XML文件第5行:错误   膨胀类android.support.design.widget.CoordinatorLayout

这是我的布局概述:

app:srcCompat"= @drawable/test_background"

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

<?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>

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

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

我是否需要在drawable中设置任何属性以将其用作活动的背景?还是我做错了什么?

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

here所述,您必须创建一个可绘制的新图层以容纳可绘制的矢量。此实现适用于Pre-Lollipop设备。具有Lollipop及更高版本的设备不需要此。我已经对其进行了测试,无论您使用的是哪种rootview,它都可以正常工作。

答案 1 :(得分:0)

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

enter image description here enter image description here

相关问题