Android中的gravity和layout_gravity有什么区别?

Android中的gravity和layout_gravity有什么区别?

问题描述:

我知道我们可以为 android:gravity android:layout_gravity 属性设置以下值:

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
  1. center
  2. center_vertical
  3. center_horizontal, etc.

但是我对这两者感到困惑.

But I am confused regarding both of these.

android:gravityandroid:layout_gravity的用法有什么区别?

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

他们的名字应该可以帮助您:

Their names should help you:

  • android:gravity设置所使用的View的内容(即其子视图)的重力.
  • android:layout_gravity设置ViewLayout相对于其父级的重力.
  • 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.

例如此处.