Difference between gravity and layout_gravity on A

2018-12-31 02:05发布

I know we can set the following values to the android:gravity and android:layout_gravity properties:

  1. center
  2. center_vertical
  3. center_horizontal, etc.

But I am confused regarding both of these.

What is the difference between the usage of android:gravity and android:layout_gravity?

20条回答
看风景的人
2楼-- · 2018-12-31 03:09

The difference

android:layout_gravity is the Outside gravity of the View. Specifies the direction in which the View should touch its parent's border.

android:gravity is the Inside gravity of that View. Specifies in which direction its contents should align.

HTML/CSS Equivalents

Android                 | CSS
————————————————————————+————————————
android:layout_gravity  | float
android:gravity         | text-align

Easy trick to help you remember

Take layout-gravity as "Lay-outside-gravity".

查看更多
闭嘴吧你
3楼-- · 2018-12-31 03:10

An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.

Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view

查看更多
登录 后发表回答