Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting center gravity of child view change layout alignment of another child view in horizontal layout?

In my horizontal LinearLayout, I set the gravity in one view to be center_vertical, and then I tried to set layout_gravity for a second view, but that view is lined up with the centered text in the first view!

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_gravity="top"
        android:background="@drawable/border"
        android:gravity="center_vertical"
        android:text="layout_gravity=top   gravity=center_vertical" >
    </TextView>            
    <TextView
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_gravity="top"
        android:background="@drawable/border"
        android:gravity="top"
        android:text="layout_gravity=top  gravity=top" >
    </TextView>
</LinearLayout>

enter image description here

And here is the same code but for a vertical layout. Notice that the desired behavior works correctly in the vertical layout.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:background="@drawable/border"
        android:gravity="center_horizontal"
        android:text="layout_gravity=left   gravity=center_horizontal" >
    </TextView>            
    <TextView
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_gravity="right"
        android:background="@drawable/border"
        android:gravity="right"
        android:text="layout_gravity=right  gravity=right" >
    </TextView>
</LinearLayout>

enter image description here

I can just use a RelativeLayout or maybe another nested LinearLayout to fix the problem. But I am asking this question because I would like to know if I am not understanding how gravity and layout_gravity works!! It is important to me that I understand how these fundamental attributes work. Thanks

like image 591
Lou Morda Avatar asked Sep 29 '14 04:09

Lou Morda


2 Answers

If you want one centered and one top then you need to add a baseline aligned flag to the LinearLayout

android:baselineAligned="false"

It is default for the layout to align the children's baselines. This can cause issues such as this when the children use different values of gravity.

The difference with vertical is that the baselines can't be aligned as they can in horizontal.

See:

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:baselineAligned

Additional info - This appears to explain the issue better than I can:

http://www.doubleencore.com/2013/10/shifty-baseline-alignment/

like image 80
CmosBattery Avatar answered Nov 15 '22 04:11

CmosBattery


if you are trying something not "connected to each other" i.e. you want some space between two widgets then use a blank widget and use weights

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" >

<TextView
    android:layout_width="100dp"
    android:layout_height="200dp"

    android:layout_weight="40"
    android:gravity="center_vertical"
    android:text="layout_gravity=top   gravity=center_vertical" >
</TextView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="20" />

<TextView
    android:layout_width="100dp"
    android:layout_height="200dp"

    android:layout_weight="40"
    android:gravity="right"
    android:text="layout_gravity=top  gravity=top" >
</TextView>

</LinearLayout>

Main Answer Image

You werent clear about the output that you wanted , this is what i could interpret as your required output.. But i will explain you how this works .. For more on weights you can go to http://developer.android.com/reference/android/widget/package-summary.html

There is something called as "widget" and "content of widget" While talking of linearlayout with layout_gravity you set gravity of it self as a "widget" in the main View and with gravity you set gravity to the content in that linearlayout

Simmilarly with say textview , if you use layout_gravity (say equal to center) then the textview will be centered in its space (i.e. it will be centered in its width if layoutorientation is vertical , and it will be centered in its height if layout_orientation is horizontal)

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

<TextView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:text="Second Activity" />



</LinearLayout>

Horizontal linear_layout

See the same in vertical orientation

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:text="Second Activity" />

</LinearLayout>

Vertical Linear Layout

Also now see about the gravity when you apply gravity to textview, the "TEXT" in the textview is centered (if gravity=center)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    **android:gravity="center"**
    android:text="Second Activity" />

</LinearLayout>

centerTextVertical

Now you can realise that gravity sets the gravity to the content within the widget.. the "content" of a widget can be widgets itself.. LinearLayout is a widget , and within linear layout you add yet other types of widgets (button and text view) So applying gravity=center to textview sets its content(i.e. text)at the center and applying gravity to linearlayout will center its content(i.e. textview) at the center

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:gravity="center">

<TextView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Second Activity" />

</LinearLayout>

layout_gravity_center

like image 40
Mihir Solanki Avatar answered Nov 15 '22 05:11

Mihir Solanki