Android 形状 xml 如下图

Android shape xml like below image

我如何在 xml(可绘制文件夹)中创建该形状。

drawable_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="@android:color/black"
        android:centerColor="@android:color/white
        android:endColor=  "@android:color/black"
        android:angle="360"
        />
</shape>

将其添加到您的布局中 view

<View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:background="@drawable/drawable_line" />

在 activity_main 中添加这些:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:gravity="center"
        android:background="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="80dp">

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/line" />
    </LinearLayout>
</LinearLayout>

并创建可绘制的 res 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="360"
        android:centerColor="@android:color/white"
        android:endColor="@android:color/black"
        android:startColor="@android:color/black" />
</shape>