如何为Android Api Level 19和更高版本创建渐变?

时间:2018-09-11 10:55:28

标签: android svg gradient android-vectordrawable

我正在尝试为项目的toolbar添加颜色。每种工具栏都有不同的颜色。我正在尝试在我的项目中创建一个新项目。我正在使用一个新项目。但是我确实很重要。 Svg's具有渐变。我可以将它们用于api 24发布而不会出现问题。但是我不能使用相同的svg,因为它们不支持gradient下的api 24。除了撤消渐变外,我不知道该如何解决。这就是为什么我在楼下共享我的Svg之一。

我的ic_sale_toolbar.xml;

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:width="375dp"
    android:height="90dp"
    android:viewportWidth="375"
    android:viewportHeight="90">
  <path
      android:pathData="M0,0h375v90h-375z"
      android:strokeWidth="1"
      android:fillType="evenOdd"
      android:strokeColor="#00000000">
    <aapt:attr name="android:fillColor">
      <gradient 
          android:startY="186.48047"
          android:startX="357.47266"
          android:endY="-171.41406"
          android:endX="66.76172"
          android:type="linear">
        <item android:offset="0" android:color="#FF3023AE"/>
        <item android:offset="1" android:color="#FFC86DD7"/>
      </gradient>
    </aapt:attr>
  </path>
</vector>

1 个答案:

答案 0 :(得分:1)

Try this
`<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient android:endColor="@color/colorPrimary" android:startColor="@color/colorPrimaryDark"
                android:centerColor="@color/colorPrimary" android:angle="315"
                android:gradientRadius="2dp"/>
            <padding android:left="2dp" android:top="3dp" android:right="2dp"
                android:bottom="3dp"/>
        </shape>
    </item>
</selector>`