android:gravity和android:layout_gravity的区别

android:gravity和android:layout_gravity的区别

android:gravity用于设置View组件(比如TextView )的对齐方式,而android:layout_gravity用于设置Container组件(比如LinearLayout)的对齐方式。

android:gravity sets the gravity of the content of the View its used on.  设置view里面的内容对齐。

android:layout_gravity sets the gravity of the View or Layout in its parent.  设置view或layout在其父容器里面的对齐。

**android:gravity    **              Specifies how to align the text by the view’s x- and/or y-axis             when the text is smaller than the view.          
Must be one or more (separated by ‘|’) of the following constant values.

Constant
Value
Description

top
0x30
Push object to the top of its container, not changing its size.

bottom
0x50
Push object to the bottom of its container, not changing its size.

left
0x03
Push object to the left of its container, not changing its size.

right
0x05
Push object to the right of its container, not changing its size.

center_vertical
0x10
Place object in the vertical center of its container, not changing its size.

fill_vertical
0x70
Grow the vertical size of the object if needed so it completely fills its container.

center_horizontal
0x01
Place object in the horizontal center of its container, not changing its size.

fill_horizontal
0x07
Grow the horizontal size of the object if needed so it completely fills its container.

center
0x11
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

fill
0x77
Grow the horizontal and vertical size of the object if needed so it completely fills its container.

clip_vertical
0x80
Additional option that can be set to have the top and/or bottom edges of             the child clipped to its container’s bounds.             The clip will be based on the vertical gravity: a top gravity will clip the bottom             edge, a bottom gravity will clip the top edge, and neither will clip both edges.

clip_horizontal
0x08
Additional option that can be set to have the left and/or right edges of             the child clipped to its container’s bounds.             The clip will be based on the horizontal gravity: a left gravity will clip the right             edge, a right gravity will clip the left edge, and neither will clip both edges.

start
0x00800003
Push object to the beginning of its container, not changing its size.

end
0x00800005
Push object to the end of its container, not changing its size.

          This corresponds to the global attribute          resource symbol gravity.
**android:layout_gravity    **              Standard gravity constant that a child can supply to its parent.         Defines how to place the view, both its x- and y-axis, within its parent view group.          
Must be one or more (separated by ‘|’) of the following constant values.

Constant
Value
Description

top
0x30
Push object to the top of its container, not changing its size.

bottom
0x50
Push object to the bottom of its container, not changing its size.

left
0x03
Push object to the left of its container, not changing its size.

right
0x05
Push object to the right of its container, not changing its size.

center_vertical
0x10
Place object in the vertical center of its container, not changing its size.

fill_vertical
0x70
Grow the vertical size of the object if needed so it completely fills its container.

center_horizontal
0x01
Place object in the horizontal center of its container, not changing its size.

fill_horizontal
0x07
Grow the horizontal size of the object if needed so it completely fills its container.

center
0x11
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

fill
0x77
Grow the horizontal and vertical size of the object if needed so it completely fills its container.

clip_vertical
0x80
Additional option that can be set to have the top and/or bottom edges of             the child clipped to its container’s bounds.             The clip will be based on the vertical gravity: a top gravity will clip the bottom             edge, a bottom gravity will clip the top edge, and neither will clip both edges.

clip_horizontal
0x08
Additional option that can be set to have the left and/or right edges of             the child clipped to its container’s bounds.             The clip will be based on the horizontal gravity: a left gravity will clip the right             edge, a right gravity will clip the left edge, and neither will clip both edges.

start
0x00800003
Push object to the beginning of its container, not changing its size.

end
0x00800005
Push object to the end of its container, not changing its size.

          This corresponds to the global attribute          resource symbol layout_gravity.

从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。
android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。
比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。