user2111639
user2111639

Reputation: 297

The difference between vertical and horizontal orientation about layout_gravity and gravity

I have a TextView in a LinearLayout.

I want to try different modes for layout_gravity and gravity.

I have this code:

<LinearLayout
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:background="#A55C93"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/textView1"
        android:background="#005Cff"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="50dp"
        android:layout_height="25dp"
        android:text="text"
        android:textSize="21sp" />
</LinearLayout>

When I have android:orientation="vertical", everything about layout_gravity and gravity makes sense. But when I have android:orientation="horizontal", outputs are different from the first, why?

Upvotes: 0

Views: 444

Answers (2)

Ben P.
Ben P.

Reputation: 54194

The main thing to understand is that layout_gravity on a child view is an attribute that communicates "up" to the parent, and "asks" the parent to behave in a certain way.

In this case, the parent is a LinearLayout, which has a different attribute (orientation) that already affects the positioning of child views. Therefore, when a child specifies layout_gravity, what happens depends on the LinearLayout's orientation.

  • For a horizontal LinearLayout, any horizontal component of the child's layout_gravity is ignored.
  • For a vertical LinearLayout, any vertical component of the child's layout_gravity is ignored.

You're using center, which is basically center_vertical + center_horizontal. So for a horizontal LinearLayout, the horizontal centering will be ignored. And, for a vertical LinearLayout, the vertical centering will be ignored.

Given that your example only has one child view, you could consider replacing the parent LinearLayout with a FrameLayout. Then the child layout_gravity will be fully respected.

Upvotes: 1

vikram
vikram

Reputation: 21

try this code and you will see the difference increase your layout height to see the difference; it will help you understand the flow better

when you set gravity center to a horizantal linear layout the center does not refer to center horizantal, it refers to center vertically.

i know it sounds confusing but try this code and you will have a better idea of what im trying to say.

<LinearLayout
android:layout_width="150dp"
android:layout_height="500dp"
android:background="#A55C93"
android:orientation="horizontal"
>
<TextView
    android:id="@+id/textView1"
    android:background="#005Cff"
    android:layout_gravity="center"
    android:layout_width="50dp"
    android:layout_height="25dp"
    android:text="text"
    android:textSize="21sp"
    />
</LinearLayout>

also try this with vertical

<LinearLayout
android:layout_width="150dp"
android:layout_height="500dp"
android:background="#A55C93"
android:orientation="vertical"
  tools:ignore="MissingConstraints"

  >
<TextView
    android:id="@+id/textView1"
    android:background="#005Cff"
    android:layout_gravity="center"
    android:layout_width="50dp"
    android:layout_height="25dp"
    android:text="text"
    android:textSize="21sp"
    />
</LinearLayout>

try adding multiple views to the layout and it will help you give a precise idea. in other terms in a horizantally oriented linear layout if you set layout gravity center is will align your view at center of height of Linear layout and if you set layout gravity center to a vertically aligned linear layout it will align your view center to the width of linear layout

Upvotes: 1

Related Questions