Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a LinearLayout and "percentage" margins

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>
like image 914
TotoTitus Avatar asked Oct 08 '12 10:10

TotoTitus


People also ask

How do you center LinearLayout in RelativeLayout?

When using the RelativeLayout , you can use android:layout_centerInParent="true" , which will do what it says, center it inside its parent.

How do you align a linear layout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do you set margins in Java?

We can set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.


2 Answers

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>
like image 106
Pratik Avatar answered Sep 29 '22 19:09

Pratik


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>
like image 41
Ahmad Aghazadeh Avatar answered Sep 29 '22 20:09

Ahmad Aghazadeh