21

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
DixieFlatline
  • 7,895
  • 24
  • 95
  • 147
  • 1
    What do you mean not working? Like they are both left aligned...? – chustar Jan 24 '11 at 15:50
  • Yes they are both left aligned. – DixieFlatline Jan 24 '11 at 16:01
  • 1
    if you have the linearlayout orientation set to 'horizontal', gravity left or right does not work. Setting it to vertical wil make this possible. I am just not sure what you are trying to accomplish here... – Boy Apr 02 '13 at 16:48

3 Answers3

66

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>    
Anm
  • 3,291
  • 2
  • 29
  • 40
Cristian
  • 198,401
  • 62
  • 356
  • 264
0

Adding to the answer by @Cristian the same thing can also be done using Linear Layout by extensive use of the attributes - "weight" and "weight_sum" (although a more tedious approach) -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="1dp"
    tools:context=".MainActivity"
    android:weightSum="5"
    android:padding="10dp"
    android:id="@+id/parent_linear_layout">

    <!-- Sub #1 Linear Layout #1 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="5"
        android:id="@+id/sub_1_linear_layout_1">

        <!-- Sub #2 Linear Layout #1 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_1">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:src="@drawable/stack_overflow"/>

        </LinearLayout>

        <!-- Sub #2 Linear Layout #2 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_2">

        </LinearLayout>

        <!-- Sub #2 Linear Layout #3 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_3">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/stack_overflow"
                android:textColor="#111111"/>

        </LinearLayout>

        <!-- Sub #2 Linear Layout #4 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_4">

        </LinearLayout>

        <!-- Sub #2 Linear Layout #5 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_5">

        </LinearLayout>

    </LinearLayout>

    <!-- Sub #1 Linear Layout #2 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="5"
        android:id="@+id/sub_1_linear_layout_2">

        <!-- Sub #2 Linear Layout #1 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_1_2">

        </LinearLayout>

        <!-- Sub #2 Linear Layout #2 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_2_2">

        </LinearLayout>

        <!-- Sub #2 Linear Layout #3 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_3_2">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/back"
                android:id="@+id/back_button"/>

        </LinearLayout>

        <!-- Sub #2 Linear Layout #4 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_4_2">

        </LinearLayout>

        <!-- Sub #2 Linear Layout #5 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/sub_2_linear_layout_5_2">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/home"
                android:id="@+id/home_button"/>

        </LinearLayout>

    </LinearLayout>

    <!-- Sub #1 Linear Layout #3 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="1"
        android:id="@+id/sub_1_linear_layout_3">

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:prompt="@string/random_prompt"/>

    </LinearLayout>

    <!-- Sub #1 Linear Layout #4 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="1"
        android:id="@+id/sub_1_linear_layout_4">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#111111"
            android:text="@string/random_text"/>

    </LinearLayout>

    <!-- Sub #1 Linear Layout #5 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="1"
        android:id="@+id/sub_1_linear_layout_5">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#111111"
            android:text="@string/test_button"/>

    </LinearLayout>

</LinearLayout>

NOTE: Since API level 8 "fill_parent" has been deprecated and "match_parent" is used instead. See - What is the difference between match_parent and fill_parent?

Payel Senapati
  • 1,134
  • 1
  • 11
  • 27
-21
android:layout_marginLeft="5dip"

should be (dp not dip)

android:layout_marginLeft="5dp"
chustar
  • 12,225
  • 24
  • 81
  • 119
  • 11
    They are equivalent actually, and that won't solve the problem. – Cristian Jan 24 '11 at 15:52
  • 2
    In fact it's been reported anecdotally that dp sometimes fails to work where dip works, despite documentation stating they are the same. – Zulaxia Apr 12 '11 at 08:20