2014-10-01 40 views
1

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

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

enter image description here

+0

如何使用@drawable图像? – bwegs 2014-10-01 23:56:38

+0

我不介意,是否有一个发电机,但可以为我做这个快? – adrian 2014-10-01 23:58:10

+0

对不起,我不是说我知道 – bwegs 2014-10-02 00:04:30

回答

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> 

和截图:

enter image description here

在该例子中有九个虚线。通过相对于中心线上下移动每条新线,您可以尽可能多地放置这些线,其中android:top="0dp"属性。

这是关于Shape Drawable的更多信息。