How do I create a ListView with dashed / dotted line dividers in Android?

时光毁灭记忆、已成空白 提交于 2020-01-01 04:16:28

问题


I managed to figure out how to create a custom shape (with a dashed stroke) by creating a file called dash.xml inside of the /app/res/drawable/ folder:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:color="#534b4b"
        android:width="1dp"
        android:dashGap="2dp"
        android:dashWidth="1dp"
    />
    <size
        android:height="1dp"
    />
</shape>

Now I'm confused as to how to apply this shape to a ListView. I've tried the following, but no divider is displayed:

<ListView android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/dash"
    android:dividerHeight="1dp"
/>

Wtf?


回答1:


Here is mine and it works :

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:color="#FF404040"
        android:width="1dp"
        android:dashGap="3dp"
        android:dashWidth="1dp"
    />
    <size
        android:height="3dp"
    />
</shape>



回答2:


You also need android:dividerHeight. Shapes are variable size, and right now you have a zero-height divider.




回答3:


  1. Define layerType for show the dotted line divider don't forget to give divider height.

<ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="@drawable/dash" android:dividerHeight="1dp" android:layerType="software"/>



来源:https://stackoverflow.com/questions/4120503/how-do-i-create-a-listview-with-dashed-dotted-line-dividers-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!