且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中制作自定义可绘制形状

更新时间:2023-02-01 22:51:03

使用 layer-list 使此自定义形状成为 drawable .

Use layer-list to make this custom shape drawable.

/res/drawable/custom_shape.xml:

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

    <!-- Transparent Rectangle -->
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="300dp"
                android:height="60dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <!-- Colored Rectangle -->
    <item
        android:bottom="20dp">
        <shape android:shape="rectangle">
            <size
                android:width="300dp"
                android:height="60dp" />
            <solid android:color="#9A8585" />
        </shape>
    </item>

    <!-- Bottom Triangle -->
    <item
        android:left="80dp"
        android:right="120dp"
        android:top="0dp"
        android:bottom="30dp">
        <rotate android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#9A8585" />
            </shape>
        </rotate>
    </item>

    <!-- Top Border -->
    <item
        android:bottom="75dp">
        <shape android:shape="rectangle">
            <solid android:color="#EFC1B3" />
        </shape>
    </item>

</layer-list>

使用情况:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_shape"/>

输出:

希望这会有所帮助〜