Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout gravity left-right

Tags:

android

layout

I have a linearlayout with two linearlayouts inside. Both linearlayouts have 2 buttons. I want to align the first two buttons to the left, and the other two buttons to the right. Unfortunately I have the four buttons (or the 2 layouts) side by side.

This is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@drawable/vat_header_selector"
        android:orientation="horizontal" >

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_numbers"
                android:layout_width="90dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector"
                android:text="Numbers">
            </Button>
            <Button
                android:id="@+id/btn_text"
                android:layout_width="90dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector2"
                android:text="Words">
            </Button>   
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_black"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector"
                android:text="B">
            </Button>
            <Button
                android:id="@+id/btn_white"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector2"
                android:text="W">
            </Button>   
        </LinearLayout>

    </LinearLayout>

This is how it looks like: enter image description here

And this is what I want: enter image description here

like image 811
erdomester Avatar asked Dec 16 '22 19:12

erdomester


1 Answers

Gravity left, right doesn't work when the orientation of the LinearLayout is set to horizontal(as the views are placed along the X axis). One way to do what you want is to insert an empty View between the two LinearLayouts to create a space between them:

<View android:layout_width="0dp"
      android:layout_height="fill_parent"
      android:layout_weight="1" />
like image 115
user Avatar answered Jan 08 '23 01:01

user