You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:如何创建圆形圆点样式的虚线?

实现圆形圆点虚线的解决方案

你当前使用line形状的stroke生成的虚线是方形端点,因为stroke的线段是矩形结构,所以dash呈现方形。要实现圆形圆点的虚线,可以通过以下方式修改:

步骤1:创建单个圆形圆点的Drawable

在res/drawable/下新建dot_shape.xml,定义单个圆形:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!-- 设置圆点颜色 -->
    <solid android:color="#FF0000" />
    <!-- 设置圆点大小 -->
    <size
        android:width="3dp"
        android:height="3dp" />
</shape>

步骤2:创建重复排列的圆点虚线Drawable

新建dotted_line.xml,利用bitmap的tileMode属性重复排列圆点,实现虚线效果:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/dot_shape"
    <!-- 开启水平重复模式 -->
    android:tileMode="repeat"
    android:tileModeX="repeat"
    android:tileModeY="disabled"
    <!-- 圆点居中对齐 -->
    android:gravity="center_vertical"
    <!-- 设置圆点之间的间距 -->
    android:spacing="4dp" />

垂直方向的圆点虚线

如果需要垂直的虚线,用rotate标签包裹上述bitmap即可:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:pivotX="50%"
    android:pivotY="50%">
    <bitmap
        android:src="@drawable/dot_shape"
        android:tileMode="repeat"
        android:tileModeX="repeat"
        android:tileModeY="disabled"
        android:gravity="center_horizontal"
        android:spacing="4dp" />
</rotate>

注意事项

  • 修正你原有代码中的拼写错误:android:fromDeegres应改为android:fromDegrees,android:share应改为android:shape。
  • 可通过调整dot_shape.xml中的size和dotted_line.xml中的spacing参数,匹配你需要的圆点大小和间距。

内容的提问来源于stack exchange,提问作者Dawid Fendler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 13:35:04