2013-11-04 72 views
3

我想用xml绘制垂直虚线,使用shape绘制垂直虚线

我用这个例子:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line"> 

<size 
android:height="400dp" 
android:width="4dp"/> 

<stroke 
     android:color="#000000" 
     android:dashWidth="100dp" 
     android:dashGap="10dp" /> 
</shape> 

然后:

<View android:id="@+id/vertical_line" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/dotted_line" 
     android:layout_marginLeft="27dp"/> 

但它不工作。我该怎么做?

回答

3

试试这个

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line" > 

    <size 
     android:height="400dp" 
     android:width="4dp" /> 

    <stroke 
     android:dashGap="10dp" 
     android:dashWidth="100dp" 
     android:color="#000000" /> 

</shape> 

然后使用这样

<LinearLayout 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:background="@drawable/dot_line" 
    android:orientation="vertical" > 
</LinearLayout> 
+0

<查看机器人:ID = “@ + ID/vertical_line” 机器人:layout_width = “WRAP_CONTENT” 机器人:layout_height = “WRAP_CONTENT” 机器人:背景= “@绘制/ dotted_line” 机器人:layout_marginLeft =“27dp”/> – IgorB

+0

我是这样使用的<但它有些如何绘制在RelativeLayout的中心,我无法理解为什么( – IgorB

+0

要绘制它的位置) –

-1
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="90" 
    android:toDegrees="90" > 

    <shape android:shape="line" > 

    <stroke 
     android:color="#000000" 
     android:dashWidth="100dp" 
     android:dashGap="10dp"/> 
    </shape> 

</rotate> 
+0

似乎在Android M和更高版本中使用旋转绘图时存在一个错误,如下所示:https://stackoverflow.com/a/8716798/3849039 – AnupamChugh

2

我尝试了一些东西,我很喜欢绘制的解决方案,发现这工作。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:top="-8dp" 
    android:bottom="-8dp" 
    android:left="-8dp"> 
    <shape> 
     <solid android:color="@android:color/transparent"/> 
     <stroke 
      android:width="4dp" 
      android:color="#ffffff" 
      android:dashGap="4dp" 
      android:dashWidth="4dp"/> 
    </shape> 
</item> 

诀窍是负顶部底部和左侧的值,因为这些被设定的物体的外现,留下一个单点划线。

它在下面的视图中使用。

<View 
android:layout_width="4dp" 
android:layout_height="match_parent" 
android:background="@drawable/dash_line_vertical" 
android:layerType="software" /> 
+0

这是唯一一个有效的工具。谢谢! – mohax

+1

很高兴^^ –