【问题标题】:How do I create a gradient for Android Api Level 19 and later?如何为 Android Api Level 19 及更高版本创建渐变?
【发布时间】:2018-09-11 10:55:27
【问题描述】:

我正在尝试为项目的toolbar 添加颜色。每个都有不同的工具栏颜色。我正在尝试在我的项目中创建一个新项目。我正在使用一个新项目。但我确实有大事。 Svg's 有渐变。我可以毫无问题地将这些用于发布 api 24。但我不能使用相同的 svg,因为它们不支持 api 24 下的 gradient。除了撤消渐变外,我不知道如何解决这个问题。这就是为什么我要在楼下分享我的一个 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>

【问题讨论】:

    标签: android svg gradient android-vectordrawable


    【解决方案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>`
    

    【讨论】:

    • 好的,感谢您告诉我。如果你用错了,请只使用渐变标签而不是整个代码。
    • 请注意,gradient type="radial" 对 api 级别 19 没有影响。
    猜你喜欢
    • 2016-12-21
    • 2015-09-11
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2021-11-17
    相关资源
    最近更新 更多