Difference Between Gravity and Layout_Gravity in Android

What is the difference between gravity and layout_gravity in Android?

Their names should help you:

  • android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on.
  • android:layout_gravity sets the gravity of the View or Layout relative to its parent.

And an example is here.

Difference between android:layout_gravity and android:gravity

android:layout_gravity is the gravity related to your whole Layout like LinearLayout,Relative layout and android:gravity is the gravity related to your particular view like Button,EditText..etc
Hope this helps

What is exact difference between gravity and layout_gravity

  1. Yes, your understanding is correct. Any layout_ attribute gives instructions to the parent view.
  2. Generally, if a view is taking up the full width/height of its' parent, then you won't need to specify layout_gravity, you'll probably find gravity more useful. If your view does not take up an entire dimension of its' parent, you might want to align it depending on your desired look.

What is the difference between android:gravity and android:layout_gravity

android:gravity sets the gravity of the content of the View its used on.

android:layout_gravity sets the gravity of the View or Layout in its parent.

check https://stackoverflow.com/a/6819801/1434631

LinearLayout android:layout_gravity=center_vertical works at design time but doesn't at run time

In your LinearLayout, change layout_gravity to just gravity

The difference between vertical and horizontal orientation about layout_gravity and gravity

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.

What is the difference between gravity=center and textAlignment=center and centralHorizontal=true

For practicality purposes, textAlignment and gravity are equivalent, except that textAlignment is a member of the View Class and the gravity is a member of TextView class. layout_centerHorizontal defines how a view should be placed in relation to it's parent view, while the former two deal with how the view should lay out it's subviews.



Related Topics



Leave a reply



Submit