【问题标题】:Shape dashed line, dash doesn't work形状虚线,破折号不起作用
【发布时间】:2016-12-29 11:36:39
【问题描述】:

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

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

如何解决?

【问题讨论】:

    标签: android android-shape


    【解决方案1】:

    试试这个

    在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"
    

    【讨论】:

    • android:layerType="software" - 只有这一行对我有帮助,即使是我的 XML。请写为答案
    • 嗨@Artem android:layerType="software" 给出虚线“硬件”用于平面线
    • 用于停止硬件加速的软件。
    【解决方案2】:

    添加这个drawable: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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2021-12-28
      • 2021-08-14
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多