Android 中的虚线 XML

Dotted line XML in Android

我正在开发一个 Android 应用程序,我想在其中使用虚线 XML 作为布局中的分隔线。为此,我为此使用了不同的可绘制对象,而不是制作一条虚线,它正在制作一条线。

我的可绘制对象如下:

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

Dashed lines are not supported in GL mode. 所以添加

android:layerType="software"

例如

<ImageView
    android:layerType="software" // add here
 ...

在您的 xml 布局中查看或编程为

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

或者像这样关闭硬件加速:

android:hardwareAccelerated="false"

可能对你有帮助..

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

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

参考这个.. How do I make a dotted/dashed line in Android?

试试这个

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

    </shape>
use Below code

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:toDegrees="90" >

    <shape android:shape="line" >

        <stroke

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

</rotate>