layout_gravity in LinearLayout

DixieFlatline picture DixieFlatline · Jan 24, 2011 · Viewed 38.2k times · Source

This is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="5dip" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffffff" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/nazajGumb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/roaming_backbtn" >
        </Button>

        <Button
            android:id="@+id/homeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/roaming_homebtn" >
        </Button>
    </LinearLayout>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dip"
        android:prompt="@string/roaming_spinnerPrompt" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Random text"
        android:textColor="#ffffcc" />

    <Button
        android:id="@+id/testBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="test" >
    </Button>
</LinearLayout>

The positioning of ImageView and TextView in LinearLayout2 and positiong of buttons in LinearLayout3 is not working (using layout gravity).

What am i missing here?

Answer

Cristian picture Cristian · Jan 24, 2011

That's not the way in which android:layout_gravity works. Both, left and center_horizontal parameters work only when the android:orientation is vertical. To achieve what you want, you better use RelativeLayout:

  <RelativeLayout
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <ImageView   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dip"/>

        <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"  
        android:layout_centerHorizontal="true"
        android:textColor="#ffffff"/>
  </RelativeLayout>