画垂直虚线

时间:2013-11-04 08:37:41

标签: android

我想使用shape在xml中绘制一条垂直虚线。

我用过这个例子:

<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 个答案:

答案 0 :(得分:7)

我尝试了一些东西,我喜欢可绘制的解决方案,并发现这很有效。

<?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="#df0000"
            android:dashGap="4dp"
            android:dashWidth="4dp"/>
    </shape>
</item>
</layer-list>

技巧是负的顶部底部和左侧值,因为它们现在设置在对象之外,只留下一条虚线。

用于以下视图。

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

答案 1 :(得分: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>

答案 2 :(得分:-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>
相关问题