【问题标题】:Android: multiple dashed line background using Shape DrawableAndroid:使用Shape Drawable的多个虚线背景
【发布时间】:2014-11-26 22:49:43
【问题描述】:

有没有办法使用可绘制的形状或类似的东西来为视图创建虚线背景?

理想情况下,我想要这样的东西(当然有不同的颜色)。忽略蓝色边框,我希望红线理想地重复作为视图的背景。

【问题讨论】:

  • 使用@drawable 图像怎么样?
  • 我只是希望能够制作像 /////// 这样的重复背景,而无需通过 .9 补丁或图像箍。是的,我的意思是希望在没有图像的情况下平铺背景。看来我必须使用图像解决方案。
  • 理想情况下任何通过 XML 定义的东西,我现在只是使用图像,而是使用可正确缩放的绘图

标签: android android-shapedrawable


【解决方案1】:

这是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle">
        <solid android:color="#ffffff" />
    </shape>
</item>

<item
    android:top="-400dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="-300dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="-200dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="-100dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="0dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="100dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="200dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="300dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>

<item
    android:top="400dp">
    <shape android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000"
            android:dashWidth="10dp"
            android:dashGap="10dp" />
    </shape>
</item>
</layer-list>

还有截图:

在示例中,有九条虚线。您可以通过相对于中心线(具有 android:top="0dp" 属性的那条)上下移动每条新线来放置任意数量的新线。

这里有更多关于Shape Drawable的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多