Android - XML 自定义形状

Android - XML Custom shape

我正在尝试在 xml 中完成此形状:

我试过的:

 <item>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@color/res_pressed"/>
        <item
            android:drawable="@color/res_default"
            android:top="0dp"
            android:right="0dp"
            android:bottom="0dp"
            android:left="15dp"/>
    </layer-list>
</item>

我仍然无法正确处理黄色形状的圆角和直角。

您需要使用 shape 可绘制对象,以便添加角标记。尝试

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/res_pressed" />
            <corners android:topLeft="10dp" android:bottomLeft="10dp" />
        </shape>
    </item>
    <item
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        android:left="15dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/res_default" />
            <corners android:topRight="10dp" android:bottomRight="10dp" />
        </shape>
    </item>
</layer-list>

这是一个解决方法:

创建top.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimaryDark" />
    <corners
        android:radius="15dp"/>
 </shape>

创建bot.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorAccent" />
    <corners android:radius="15dp" />

</shape>

想要的形状:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bot" />
    <item
        android:bottom="0dp"
        android:drawable="@drawable/top"
        android:left="15dp"
        android:right="0dp"
        android:top="0dp"/>
</layer-list>

试试这个,与 Gabe Sechan post 相同,但稍作修改..

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="20dp"
        >
        <shape android:shape="rectangle">
            <solid android:color="@color/yellow"/>
            <corners
                android:bottomLeftRadius="10dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="10dp"
                android:topRightRadius="0dp"
                />
        </shape>
    </item>
    <item
        android:bottom="0dp"
        android:left="15dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/blue"/>
            <corners
                android:bottomLeftRadius="0dp"
                android:bottomRightRadius="10dp"
                android:topLeftRadius="0dp"
                android:topRightRadius="10dp"/>
        </shape>
    </item>
</layer-list>