在Android布局中创建水平虚线

7
在我的布局中,我想要绘制一条虚线。为了画一条水平线,我在我的布局文件中定义了一个视图。
     <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@drawable/customdots" />

以及 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

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

但是使用这段代码后我没有得到任何结果,请帮帮我。
当我将customdots.xml用作listView分隔符时,发现没有效果。
 android:divider="@drawable/customdots"

它展示了一条清晰的点线


android:left="10dp" android:right="10dp" android:width="4dp"这个有意义吗? - pskink
@pskink,你认为应该是什么? - test
2个回答

52

在这个问题上我也曾感到很困惑,直到我发现最新版本的 Android 存在一个 bug,在渲染像这样的线条时会出现问题。

可以通过将 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>

layout.xml:

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

2
android:layerType="software" 是关键。 - TootsieRockNRoll
24
似乎在Android 5.0上无效,没有任何显示。 - fifth
3
只有当“stroke”具有“width”属性而非“height”属性时,此代码才能正常工作。同时还需要“layerType”。 - Adrien Cadet
2
@kasimir 为什么你在使用 px 而不是 dp - IgorGanapolsky
2
但是,为什么是layerType? - Nigam Patro
显示剩余3条评论

0
你可以使用以下代码。它可能会对你有所帮助。 在drawable文件夹中创建一个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>

然后在您的布局中使用此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>

1
我仍然看不到明显的线条。它显示一个没有图像的图像视图。我的上面的代码也是一样的情况。 - test
同样的问题。也许这在旧版的Android上可以工作? - Marty Miller
@SAURABH,为什么你在分隔符中使用ImageView而不是View? - IgorGanapolsky

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