Best practices for spacing between elements in LinearLayout
Asked Answered
O

3

5

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?

Overpraise answered 10/10, 2017 at 15:24 Comment(3)
What about using a view and place it between your views?Bacterin
@MohammadZarei Do you mean solution described here: https://mcmap.net/q/125618/-how-to-make-space-between-linearlayout-childrenOverpraise
I would suggest to go with the second one: you might want to change the TextView in some custom class in the future, and you can embed that in the class. Or you might want to create a style resource for that TextView and define margins in the style.Lip
O
12

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" />
Operetta answered 10/10, 2017 at 15:33 Comment(0)
M
5

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>
Mathers answered 10/10, 2017 at 16:2 Comment(0)
J
2

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.

Joby answered 10/10, 2017 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.