Best practices for spacing between elements in LinearLayout

10,085

Solution 1

Try this .

<?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="wrap_content"
android:orientation="horizontal">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello" />

<android.support.v4.widget.Space
    android:layout_width="8dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello" />

<android.support.v4.widget.Space
    android:layout_width="8dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello" />
</LinearLayout>

Add space to your code .

<android.support.v4.widget.Space
    android:layout_width="8dp"
    android:layout_height="wrap_content" />

Solution 2

Either the given solutions, you can also use a drawable divider for LinearLayout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle"
    android:divider="@drawable/divider8p"
    android:orientation="vertical">

    <!-- Your items here -->

</LinearLayout>

and for the divider declaration:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#00FFFFFF"/>
    <size android:width="8dp" android:height="8dp"/>
</shape>

Solution 3

I like the Space solution already posted, but I wanted to add another answer in the sprit of the original question.

In the case the OP presented, if I were to use margins to accomplish this, I would use start/left margin on each element other than the first. I would do this because of how I predict the layout might change in the future. It seems to me that the most likely thing to happen would be to add new elements to the end of the LinearLayout or to remove elements from the end of the LinearLayout. In these cases, if I'm using start/left margin on each item, I can delete individual views without having to touch the others.

Share:
10,085

Related videos on Youtube

Piotr Aleksander Chmielowski
Author by

Piotr Aleksander Chmielowski

Updated on September 20, 2022

Comments

  • Piotr Aleksander Chmielowski
    Piotr Aleksander Chmielowski over 1 year

    I've created a simple LinearLayout with three identical elements:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hello"/>
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hello"/>
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hello"/>
    </LinearLayout>
    

    Now I'm going to introduce 8dp space between each pair of elements.

    Which solution of the ones below are considered as cleaner?

    enter image description here or: enter image description here

    or maybe some another?