如何在可绘制对象 xml 中创建垂直线? Android

How can I create a vertical line in a drawable xml? Android

有没有办法制作绘制垂直线的可绘制对象?到目前为止,我有一条水平线绘制了这个:

当前代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/object_switcher_background" />
    <item android:left="0dp"
          android:right="250dp">
        <shape android:shape="line">

            <stroke android:color="@color/c19"
                android:width="2dp"/>
            <size android:width="40dp"/>
        </shape>

    </item>
    <item android:left="50dp"
          android:bottom="50dp"
          android:top="75dp"
          android:right="150dp">
          <shape android:shape="line">

              <stroke android:color="@color/c19"
                  android:width="2dp"
                  android:rotation="90"/>
              <size android:width="40dp"/>
          </shape>

    </item>
</layer-list>

我怎样才能让它看起来像这样?

您可以将第二个项目嵌套在 rotate 个项目中。

我不确定你为什么需要一个 drawable。

这也可以通过使用一个简单的视图并根据您的需要调整其高度、宽度和位置来完成。例如:

 <View
      android:layout_width="1dp"
      android:background="@color/grey_text_color"
      android:layout_height="match_parent"/>

您可以使用旋转项目来做到这一点:下面的代码定义了一个带有垂直分隔线的白色背景,仅使用 xml drawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item>
        <rotate android:fromDegrees="90">
            <shape android:shape="line">
                <stroke
                    android:width="1dp"
                    android:color="@android:color/darker_gray" />
            </shape>
        </rotate>
    </item>