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

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

问题描述:

我知道我们可以为 android:gravityandroid: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_horizo​​ntal

但我对这两者感到困惑.

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.

一个例子是这里.