在安卓中创建水平和垂直虚线

49

我想使用形状在Android中绘制水平和垂直虚线。

我想要绘制像这样的线条:

enter image description here

对于水平线:

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

    <stroke
        android:dashGap="6px"
        android:dashWidth="6px"
        android:color="#C7B299" />

</shape>

对于竖直线

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
<size
     android:height="400dp"/>
    <stroke
        android:dashGap="6px"
        android:dashWidth="6px"
        android:color="#C7B299" />

</shape>

但是垂直虚线没有显示,我的输出结果就像这样:

在这里输入图片描述

如何绘制垂直线。

12个回答

0
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="90"
        android:toDegrees="90">
    <shape android:shape="line">
        <stroke
                android:color="@color/darkGray"
                android:width="1dp"
                android:dashGap="4dp"
                android:dashWidth="2dp"/>
    </shape>
</rotate>

<View
                android:layerType="software"
                android:background="@drawable/bg_vertical_dash_gray_1dp"
                android:layout_width="@dimen/_15sdp"
                android:layout_height="@dimen/_30sdp"/>

以上代码能够正常工作的关键是使用 android:layerType="software"。 如需了解更多信息,请查看this链接。

-1

这个解决方案是100%有效的,希望能帮到你:

首先创建一个可绘制对象,用于绘制水平虚线。

Let dashed line drawable name is horizontal_dashed_line.xml

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

     <stroke
      android:width="3dp"
      android:color="#80ffffff"
      android:dashWidth="20dp"
      android:dashGap="5dp" />
  </shape>

如果你想要有垂直虚线,你需要按照以下方式旋转这个可绘制对象:

Let drawable name is vertical_dashed_line.xml

  <?xml version="1.0" encoding="utf-8"?>
  <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:toDegrees="90"
    android:pivotX="50%"
    android:pivotY="50%"
    android:drawable="@drawable/horizontal_dashed_line">

  </rotate>

现在您有一条水平和垂直的虚线。

如何使用:

要绘制水平线,只需在布局中添加horizontal_dashed_line.xml。例如:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/horizontal_dashed_line"

    </RelativeLayout>

但是如果你想要垂直线,只需添加vertical_dashed_line.xml而不是horizontal_dashed_line.xml。例如:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/vertical_dashed_line"

    </RelativeLayout>

祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接