代码之家  ›  专栏  ›  技术社区  ›  Vijayadhas Chandrasekaran

如何在android中居中渐变色

  •  0
  • Vijayadhas Chandrasekaran  · 技术社区  · 7 年前

    嗨,我正在尝试android中的渐变。 我正在尝试以下代码

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="#EF5350"
            android:centerColor="#FFEBEE"
            android:endColor="#EF5350"
            android:angle="-270" />
    </shape>
    

    结果:

    OP image

    我想增加中间白色的尺寸。请帮我解决我的问题。

    2 回复  |  直到 4 年前
        1
  •  4
  •   Vidhi Dave    7 年前

    尝试以下操作:

    enter image description here

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:top="50dp">
            <shape android:shape="rectangle">
                <gradient
                    android:startColor="#EF5350"
                    android:endColor="#EF5350"
                    android:centerColor="#FFFFFF"
                    android:angle="270"/>
                <size
                    android:height="100dp"
                    android:width="80dp"/>
            </shape>
        </item>
        <item
            android:bottom="50dp">
            <shape android:shape="rectangle">
                <gradient
                    android:startColor="#EF5350"
                    android:endColor="#FFFFFF"
                    android:centerColor="#FFFFFF"
                    android:angle="270"
                    />
                <size
                    android:height="100dp"
                    android:width="80dp"/>
            </shape>
        </item>
    </layer-list>
    
        2
  •  1
  •   Fenil Patel    7 年前

    您需要指定 类型 在渐变标记内,

    例如,在我的情况下,我定义了 径向的 对于中心渐变效果,

    android:type=“径向”

    我已经创建了带有中心渐变效果的渐变形状(centergradietbg.xml),看看我的代码

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <gradient
            android:centerColor="#c1c1c1"
            android:endColor="#4f4f4f"
            android:gradientRadius="400"
            android:startColor="#c1c1c1"
            android:type="radial" >
        </gradient>
    
    </shape>
    

    在布局背景中使用此xml

    android:background="@drawable/centergradietbg"