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

时间:2016-12-29 11:36:40

标签: android android-shape

我想使用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>

在我的设备上,我看到了这个: enter image description here 但我需要这样的事情:

enter image description here

如何解决?

2 个答案:

答案 0 :(得分:4)

试试这个

在drawable中创建一个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"

答案 1 :(得分:0)

添加此可绘制对象: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