Gravity is the center of gravity, which indicates the position of the stop.
Differences between Android: layout_gravity and Android: gravity
From the name, we can see that Android: gravity refers to the element itself, and the text of the element itself is displayed in any place by changing the attribute settings, but not set to the left by default.
Android: layout_gravity is relative to its parent element, indicating where the element is displayed.
For example, "button: Android: layout_gravity" indicates the position of the button on the interface. Android: gravity indicates the position of the word on the button.
Optional Value
Optional values of these two attributes include top, bottom, left, right, center_vertical, fill_vertical, center_horizontal, fill_horizontal, center, fill, and clip_vertical.
These attributes can be selected multiple times, separated by "|.
The default value is gravity. Left.
Value |
Description |
Top |
Put the object at the top of its container, not changing its size. Place the object on the top of the container without changing its size. |
Bottom |
Put the object at the bottom of its container, not changing its size. Place the object at the bottom of the container without changing its size. |
Left |
Put the object at the left edge of its container, not changing its size. Place the object on the left of its container without changing its size. |
Right |
Put the object at the right edge of its container, not changing its size. Place the object on the right of its container without changing its size. |
Center_vertical |
Place object in the vertical center of its container, not changing its size. Center the object vertically without changing its size. Vertical Alignment: center alignment in the vertical direction. |
Fill_vertical |
Grow the vertical size of the object if needed so it completely fills its container. When necessary, increase the vertical size of the object to be fully filled with its container. Vertical Filling |
Center_horizontal |
Place object in the horizontal center of its container, not changing its size. Center the object horizontally without changing its size. Horizontal Alignment: Align horizontally to center |
Fill_horizontal |
Grow the horizontal size of the object if needed so it completely fills its container. When necessary, increase the horizontal size of the object to be fully filled with its container. Horizontal Filling |
Center |
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. Center an object vertically without changing its size. |
Fill |
Grow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default. When necessary, increase the horizontal and vertical size of the object to fully fill the container. |
Clip_vertical |
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 is based on the vertical gravity: A top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. Additional options are used to cut the content at the top and/or bottom of the object based on the container edge. based on its vertical alignment settings: when the top is aligned, the bottom is cut; when the bottom is aligned, the top is cut; in addition, the top and the bottom are cut. Vertical crop |
Clip_horizontal |
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 is based on the horizontal gravity: A left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges. Additional options are used to cut the content on the left and/or right of the object based on the container edge. based on its horizontal alignment settings: When alignment is left, the right side is cut; When alignment is right, the left side is cut; in addition, the left side and the right side are cut. Horizontal crop |
Simple memory: horizontal is the horizontal direction of operation, that is, horizontal, vertical is the vertical direction of speculation, that is, vertical.
Question about when linearlayout will take effect
See: layout_gravity and gravity
For linearlayout
When Android: Orientation = "vertical", only the horizontal setting works, but the vertical setting does not. That is, left, right, and center_horizontal take effect.
When Android: Orientation = "horizontal", only the vertical setting works, and the horizontal setting does not. That is, top, bottom, and center_vertical take effect.