layout_gravity in LinearLayout
Asked Answered
C

3

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?

Clue answered 24/1, 2011 at 15:45 Comment(3)
What do you mean not working? Like they are both left aligned...?Vulcanism
Yes they are both left aligned.Clue
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...Drily
H
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>    
Handicraft answered 24/1, 2011 at 15:49 Comment(3)
Tnx. Is this a bug or a rule that you can't use left/center horizontal in horizontal orientation of linear layout?Clue
It's not a bug. Just a feature.Handicraft
I don't think you need android:orientation attribute in RelativeLayout.Lollard
P
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?

Pellagra answered 7/4, 2022 at 10:46 Comment(0)
V
-21
android:layout_marginLeft="5dip"

should be (dp not dip)

android:layout_marginLeft="5dp"
Vulcanism answered 24/1, 2011 at 15:49 Comment(2)
They are equivalent actually, and that won't solve the problem.Handicraft
In fact it's been reported anecdotally that dp sometimes fails to work where dip works, despite documentation stating they are the same.Tagmeme

© 2022 - 2024 — McMap. All rights reserved.