What's the difference between gravity and layout_gravity on Android?

4

In the customization of layout properties in Android Studio, I noticed that there is gravity and layout_gravity , both have the name gravity , so I'm a little confused as to its features.

That is, what are the differences between android:layout_gravity and android:gravity ?

How can each one be used, and what visual effects their properties can cause in the android application layout?

    
asked by anonymous 07.10.2016 / 22:56

2 answers

9

The difference is what refers to gravity .

  • android:gravity - Position the contents of view in relation to it.
  • android:layout_gravity - Position view in relation to the layout that contains it.

This effect can be easily checked with a TextView :

  • android:gravity="center_horizontal" - Center the text horizontally, inside the text box
  • android:layout_gravity="center_horizontal" - Centers the text box horizontally, within layout .

Imagetakenfromthis response in SOen.

The same answer mentions some aspects that must be taken into account:

  • layout_gravity has no effect on RelativeLayout , use only LinearLayout and FrameLayout .
  • gravity and layout_gravity has no effect if you use wrap_content for the TextView or layout dimensions.
07.10.2016 / 23:01
1

The difference is that:

  • android: gravity configures the view's gravity you're using - how to align elements within that view;
  • android: layout-gravity sets the view's gravity relative to its parent- specifies how this view should be aligned against View parent.
07.10.2016 / 23:02