Center a LinearLayout and "percentage" margins

10.8k Views Asked by At

Edit: I have received already two comprehensive answers regarding fixed margins. While I've decided altogether to use fixed margins instead of weight margins, the original question remains open.

I am trying to obtain the following design in Android:

Desired Layout

A centered vertical list of stuff (TextViews, EditViews etc.) which leaves about 10% of the horizontal space free as left/right margin, with background.

What I tried and did not work/worked partially:

  • LinearLayout, vertical, as top-level layout. If the gravity is set to "centered", the background is limited to the size of the layout. Also, how does one set percentage margins (widths) in this manner?
  • LinearLayout on RelativeLayout: Background works, horizontal centering works, weights don't exist.
  • LinearLayout on LinearLayout: Background works, weights work, horizontal centering pushes all available space to the right.

(In the last two cases, my Eclipse also complains that one of the layouts is redundant.)

I have not posted code, having considered that this is somewhat more of a principle-related question. What would be the (best) way of accomplishing this?

Thank you.

XML corresponding to the last one of the test case:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:weightSum="1.0"
    android:background="#013c57" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Stuff -->
    </LinearLayout>
</LinearLayout>
5

There are 5 best solutions below

0
On

You can use compile 'com.android.support:percent:24.2.1'

You can now use PercentFrameLayout or PercentRelativeLayout. They both have the following attributes:

layout_widthPercent
layout_heightPercent
layout_marginPercent
layout_marginLeftPercent
layout_marginTopPercent
layout_marginRightPercent
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent



<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt1"
        android:background="#ff7acfff"
        android:text="50% - 50% margin 25"
        android:textColor="@android:color/white"
        app:layout_marginTopPercent="10%"
        app:layout_marginLeftPercent="10%"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="80%" />

    <TextView
        android:layout_below="@id/txt1"
        android:id="@+id/txt2"
        android:background="#ff7acfff"
        android:text="50% - 50% margin 25"
        android:textColor="@android:color/white"
        app:layout_marginTopPercent="10%"
        app:layout_marginLeftPercent="10%"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="80%"/>

    <TextView
        android:layout_below="@id/txt2"
        android:id="@+id/txt3"
        android:background="#ff7acfff"
        android:text="50% - 50% margin 25"
        android:textColor="@android:color/white"
        app:layout_marginTopPercent="10%"
        app:layout_marginLeftPercent="10%"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="80%"/>

    <TextView
        android:layout_below="@id/txt3"
        android:id="@+id/txt4"
        android:background="#ff7acfff"
        android:text="50% - 50% margin 25"
        android:textColor="@android:color/white"
        app:layout_marginTopPercent="10%"
        app:layout_marginLeftPercent="10%"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="80%"/>


</android.support.percent.PercentRelativeLayout>
3
On

here is the simplest xml code for creating this type of layout check it

<?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="match_parent"
    android:orientation="vertical" 
    android:background="#000"
    android:gravity="center">

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>
</LinearLayout>
0
On

I think Relative layout is a better choice!

Try to read this thread:

Percentage width in a RelativeLayout

I hope it will help you!

0
On
<?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="match_parent"
    android:orientation="vertical"
    android:background="#000"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#000"
        android:layout_gravity="center"
        android:gravity="center">

        <EditText android:layout_width="fill_parent"
            android:margin_left="10dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <EditText android:layout_width="fill_parent"
            android:margin_left="10dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <EditText android:layout_width="fill_parent"
            android:margin_left="10dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <EditText android:layout_width="fill_parent"
            android:margin_left="10dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

try this

0
On

With the introduction of ConstraintLayout, you can set percentage margins for your views by constraint them by guidelines.

Here's the example:

Layout Editor

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/editText2"
        app:layout_constraintEnd_toStartOf="@+id/endGuideline"
        app:layout_constraintStart_toStartOf="@+id/startGuideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintBottom_toTopOf="@+id/editText3"
        app:layout_constraintEnd_toStartOf="@+id/endGuideline"
        app:layout_constraintStart_toStartOf="@+id/startGuideline"
        app:layout_constraintTop_toBottomOf="@+id/editText1" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintBottom_toTopOf="@+id/editText4"
        app:layout_constraintEnd_toStartOf="@+id/endGuideline"
        app:layout_constraintStart_toStartOf="@+id/startGuideline"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/endGuideline"
        app:layout_constraintStart_toStartOf="@+id/startGuideline"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <android.support.constraint.Guideline
        android:id="@+id/startGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.1" />

    <android.support.constraint.Guideline
        android:id="@+id/endGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.9" />

</android.support.constraint.ConstraintLayout>