简体   繁体   中英

Divider not working for ListView in Android studio

I am trying to add spacing to my list items in ListView. I have tried using divider and dividerHeight but this doesn't seem to work. Please what could be wrong?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_export_logistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.orume.export.ExportLogisticsActivity">


<include layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></include>


<ListView
    android:id="@+id/ListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:divider="@null"
    android:dividerHeight="16dp"
    android:layout_marginTop="65dp"
    android:background="#fff"></ListView>
</RelativeLayout>

divider.xml

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

<stroke
    android:width="20dp"
    android:color="#dd0d0d" />

</shape>

you don't set your divider to your ListView .
android:divider="@null" @null here means that you set the divider to empty. Now you have a drawable divider.xml , and you need to tell Android that you want it to be used as a divider.

If your divider.xml is placed under drawable directory, then use android:divider="@drawable/divider"

Ok. Found a way out. I ended up add android:layout_marginBottom to the texView that defines my ListView and it worked perfectly. Thanks.

Create a custom view for your list items and give top and bottom padding or other space adjustments inside that custom item layout to bring space for list items. An eg link is below Android custom Row Item for ListView

Also another note is giving divider height might not work well.

<ListView
    android:id="@+id/ListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="16dp"
    android:layout_marginTop="65dp"
    android:background="#fff"></ListView>

replace this listview with yours..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM