在android布局中创建一条水平虚线

tes*_*est 7 android

在我的布局中,我正在尝试绘制一个DOTTED LINE.for绘制一条水平线,我在我的布局文件中定义了一个视图.

     <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@drawable/customdots" />
Run Code Online (Sandbox Code Playgroud)

和customdots.xml

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:left="10dp"
    android:right="10dp"
    android:width="4dp"
    android:drawable="@drawable/dotted" />

 </layer-list>
Run Code Online (Sandbox Code Playgroud)

dotted.xml

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

  <size
    android:height="4dp"
    android:width="1024dp"/>
  <stroke
   android:width="4dp" 
   android:dashWidth="4dp"
   android:color="@android:color/black"
   android:dashGap="5dp"/>

</shape>
Run Code Online (Sandbox Code Playgroud)

但是我没有使用这段代码.请帮帮我.

当我在listView Divider中使用customdots.xml时

 android:divider="@drawable/customdots"
Run Code Online (Sandbox Code Playgroud)

它显示了一个很好的虚线

Ern*_*nie 50

我也在解决这个问题,直到我发现最新版本的Android在渲染这样的线条时出现了一个错误.

通过将android:layerType ="software"添加到使用虚线作为背景的视图中,可以避免此错误.

例:

dotted.xml:

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

<stroke
    android:dashGap="3dp"
    android:dashWidth="8dp"
    android:height="2px"
    android:color="#FFFFFF" />

</shape>
Run Code Online (Sandbox Code Playgroud)

layout.xml:

    <View
        android:id="@+id/vDottedLine"
        android:background="@drawable/dotted"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:layerType="software" />
Run Code Online (Sandbox Code Playgroud)

  • 它似乎不适用于Android 5.0,没有显示任何内容 (24认同)
  • 只有当`stroke`具有`width`属性而不是`height`时,此代码才有效.`layerType`也是必需的. (3认同)
  • ndroid:layerType ="software"是关键 (2认同)
  • @kasimir你为什么用`px`代替`dp`? (2认同)
  • 但是,为什么是layerType呢? (2认同)

SAU*_*_12 0

您可以使用以下代码。它可能会帮助你。在可绘制文件夹中创建一个 dotted.xml,如下所示...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
       <stroke
       android:color="#A52A2A"
       android:dashWidth="10px"
       android:dashGap="10px" />

</shape>
Run Code Online (Sandbox Code Playgroud)

然后在你的布局中使用这个 xml 和图像视图,如下所示......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is above line of code " />
    <ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:layout_marginBottom="3dp"
    android:src="@drawable/dottede" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is below code " />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)