What’s the difference between Gravity and layout_gravity on Android?

Asked

Viewed 3,434 times

5

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

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

How each can be used, and what are the visual effects its properties can cause in the layout of the android application?

2 answers

11


The difference is what it refers to Gravity.

  • android:gravity - Position the content of view in relation to her.
  • android:layout_gravity - Positions the view in regard to the layout containing.

This effect can be easily verified with a Textview:

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

inserir a descrição da imagem aqui Image taken from this reply in the Soen.

The same reply refers to some aspects that should be taken into account:

  • layout_gravity has no effect on a Relativelayout, use only in Linearlayout and Framelayout.
  • gravity and layout_gravity it has no effect if you use wrap_content for the dimensions of Textview or of layout.
  • With the image I understood better the functioning, so you can know exactly the expected result, grateful to add it.

  • 2

    All credits to Suragch for his reply on Soen.

2

The difference is that:

  • android:Gravity configures the View Gravity you are using - how the elements should be aligned within this view;
  • android:layout-Gravity configures View’s Gravity relative to its Parent-specifies how this view should be aligned relative to View Parent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.