6

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>
Onik
  • 19,396
  • 14
  • 68
  • 91
TotoTitus
  • 178
  • 1
  • 1
  • 13

5 Answers5

2

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>
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • While this does indeed preserve the background and eliminates a potentially redundant layout, how does this accomplish left and right margins? – TotoTitus Oct 08 '12 at 11:03
  • by simply adding this two statement and remove the android:layout_margin="10dp", (**android:layout_marginLeft="10dp" and android:layout_marginRight="10dp"**) – Pratik Oct 08 '12 at 11:12
  • I understand, your answer is useful, but it does not provide procent, or weight-based margins. (Which may not be a good idea anyway; fixed margins are probably better, but it is relevant to the original question). – TotoTitus Oct 08 '12 at 11:17
1

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>
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

I think Relative layout is a better choice!

Try to read this thread:

Percentage width in a RelativeLayout

I hope it will help you!

Community
  • 1
  • 1
Tam Dao
  • 435
  • 4
  • 10
0
<?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

Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
Syn3sthete
  • 4,151
  • 3
  • 23
  • 41
0

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>
Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68