形状虚线,破折号不起作用

Shape dashed line, dash doesn't work

我想用 shape 制作虚线。

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="line">
            <stroke android:color="@color/grey_divider"
                android:width="1dip"
                android:dashWidth="5dip"
                android:dashGap="13dip"/>
        </shape>
    </item>
</selector>

在我的设备上,我看到了这个: 但我需要这样的东西:

如何解决?

试试这个

在可绘制文件中创建一个 xml 文件。(例如 dashed_line.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">

    <stroke
       android:color="#C7B299"
       android:dashWidth="10px"
       android:dashGap="10px"
       android:width="1dp"/>
</shape>

现在在您的布局中使用这个 xml 文件。

android:background="@drawable/dashed_line"

添加这个可绘制对象:dash_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="#ff0000"
        android:dashWidth="2dp"
        android:dashGap="4dp" />
</shape>

然后使用drawable作为背景:

<View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/dash_line"
            android:layerType="software" />

不要忘记低 sdk 设备的属性

android:layerType="software"

这里有详细信息:https://developer.android.com/guide/topics/graphics/hardware-accel