How to set space between listView projects in Android

Keywords: Android xml encoding

I try to use marginBottom on listView to make room between listView items, but they are still attached together.

Is it possible if so, is there a specific way to do it?

My code is as follows

<LinearLayout
android:id="@+id/alarm_occurences"
android:layout_width="fill_parent" 
android:orientation="vertical"
android:layout_height="fill_parent"
android:background="#EEEEFF"
xmlns:android="http://schemas.android.com/apk/res/android">

<ListView
android:id="@+id/occurences"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

My custom list items:

<com.android.alarm.listItems.AlarmListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="@drawable/alarm_item_background"
android:layout_marginBottom="10dp"    
>
<CheckedTextView     
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif"
    android:padding="10dp"

/>

</com.android.alarm.listItems.AlarmListItem>

In this case, how do you leave a space between the list items?

#1 building

You should wrap ListView items (such as your ListView item) in other layouts, such as LinearLayout, and add margins to your ListView item:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <your_listview_item
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
    ...
    ...
    />
</LinearLayout>

In this way, you can also add spaces to the right and left side of the ListView item as needed.

#2 building

I realize that I've chosen an answer, but I just want to share the answers that will ultimately be useful to me when I encounter this problem.

I have a listView where each entry in the listView is defined by its own layout, similar to what Sammy published in his question. I tried the suggested way to change the height of the divider, but even with the invisible divider, it didn't look too beautiful. After some experimentation, I just put android:paddingBottom="5dip" into the last TextView layout element of defining a single listView entry in an XML file.

This eventually gave me the exact Android: layout? Marginbottom that I wanted to implement using Android: layout? Marginbottom. I found that this solution produced a more beautiful result than trying to increase the height of the divider.

#3 building

Although Nik Reiman's solution works, I find it's not the best I want to do. There is a problem with setting margins with separator lines, which are no longer visible, so you cannot use it to show clear boundaries between items. In addition, it does not add more "clickable areas" to each item, so if you want to make the item clickable and the item is thin, it is difficult for anyone to click on the item because the height of the divider is not part of the item.

Fortunately, I found a better solution, which can display the separator line and adjust the height of each project using margins instead of padding. Here is an example:

List display

<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Item list

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Item"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

#4 building

If you want to display borderlines with margins and without stretching, use InsetDrawable (size must be in the format, @ Nik Reiman for that format):

List display:

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separator_line"
    android:dividerHeight="10.0px"/>

@ drawable / separator_line:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="5.0px"
    android:insetRight="5.0px"
    android:insetTop="8.0px"
    android:insetBottom="8.0px">

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="@color/colorStart"
            android:centerColor="@color/colorCenter"
            android:endColor="@color/colorEnd"
            android:type="linear"
            android:angle="0">
        </gradient>
    </shape>
</inset>

#5 building

The solution for me to increase the space but keep the horizontal line is to add divider.xml in the res/drawable folder to define the line:

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="1px"
        android:color="@color/nice_blue" />

</shape>

Then reference the following separator in my list:

<ListView
    android:id="@+id/listViewScheduledReminders"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_marginBottom="@dimen/mediumMargin"
    android:layout_weight="1"
    android:divider="@drawable/divider"
    android:dividerHeight="16.0dp"
    android:padding="@dimen/smallMargin" >

</ListView>

By increasing and decreasing this height, I noticed that android:dividerHeight="16.0dp", I basically added more padding at the top and bottom of the divider line.

I use this page as a reference: http : //developer.android.com/guide/topics/resources/drawable-resource.html#stroke-element

Posted by Majes on Thu, 20 Feb 2020 02:29:04 -0800