在android布局中创建水平虚线 [英] Create a Horizontal dotted line in android layout

查看:172
本文介绍了在android布局中创建水平虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的布局中,我试图绘制虚线.为绘制水平线,我在布局文件中定义了一个视图.

in my layout I am trying to draw a DOTTED LINE.for drawing a horizontal line i am defining a view in my layout file.

     <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@drawable/customdots" />

和customdots.xml

and 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>

dotted.xml

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>

但是我没有使用此代码的任何行.请帮帮我.

But i don't get any line using this code. please help me out.

当我在listView Divider中使用customdots.xml作为

When i use customdots.xml in the listView Divider as

 android:divider="@drawable/customdots"

它显示了一条很好的虚线

it shows a good dotted line

推荐答案

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

I was pulling my hair on this issue too until I figured out there is a bug in the latest versions of Android when rendering lines like this.

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

This bug can be circumvented by adding android:layerType="software" to the view which is using the dotted line as a background.

示例:

dotted.xml:

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>

layout.xml:

layout.xml:

    <View
        android:id="@+id/vDottedLine"
        android:background="@drawable/dotted"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:layerType="software" />

这篇关于在android布局中创建水平虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆