Reputation: 6432
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?
or maybe some another?
Upvotes: 5
Views: 20206
Reputation: 9263
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>
Upvotes: 5
Reputation: 54204
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.
Upvotes: 2
Reputation: 8237
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" />
Upvotes: 12