Android矩形形状有两种不同的颜色

Pri*_*raj 3 xml android shape

矩形形状

如何通过使用带阴影的两种不同颜色来创建矩形形状?像上面的图像.

cap*_*wag 6

layer-list可用于解决此问题

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <size
                android:width="40dp"
                android:height="40dp" />
            <solid android:color="#F86F05" />
        </shape>
    </item>

    <item android:top="10dp">
        <shape android:shape="rectangle">
            <size
                android:width="30dp"
                android:height="30dp" />
            <solid android:color="#B31F19" />
        </shape>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

和截图相同.

在此输入图像描述

如果您想要渐变而不是纯色,请将实体更改为渐变并设置startColor,endColor和angle.

  • 看起来很完美.添加`渐变`以获得良好的方法 (2认同)

Ani*_*bey 6

Please create a drawable file and put the below code in it.

    <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
                <shape android:shape="rectangle" >
                    <size android:height="20dp" />
                    <solid android:color="#F66D08" />
                </shape>
            </item>
            <item android:top="50dp">
                <shape android:shape="rectangle" >
                    <gradient android:endColor="#AD1B1D"
                        android:startColor="#E2360A"
                        android:angle="270" />
                </shape>
            </item>
        </layer-list>
Run Code Online (Sandbox Code Playgroud)