1. Gravity is used to describe the location of the contents of the current view in the view.
Gravity is the position of the view (or view group) that controls its contents or contains views
2. Layout_gravity is the location of the view in its parent container view group.
This property is only valid when the parent container is linearlayout and framelayout
Gravity's Chinese meaning is "center of gravity", that is, the view horizontal and vertical docking position
Android:gravity: Is the view control itself, is used to set the view itself where the content should be displayed in the view where the default value is left.
Android:layout_gravity: Is relative to the parent element that contains the element being changed, and sets where the element is located in the parent element
For example, TextView:android:layout_gravity represents the position of TextView on the interface, android:gravity indicates where the TextView text is in TextView, and the default value is the left.
Basic option values:
| Value |
Description |
| Top |
Put the object at the top of it container, not changing its size. Places the object at the top of its container without changing its size. |
| Bottom |
Put the object at the bottom of it container, not changing its size. Places the object at the bottom of its container, without changing its size. |
| Left |
Put the object at the left edge of it container, not changing its size. Places the object on the left side of its container without changing its size. |
| Right |
Put the object at the right edge of its container, not changing its size. Places the object on the right side of its container without changing its size. |
| Center_vertical |
Place object in the vertical center of its container, not changing its size. Centers the object vertically without changing its size. Vertical alignment: Center-aligned vertically. |
| 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 fully fill its container. Vertical fill |
| Center_horizontal |
Place object in the horizontal center of its container, not changing its size. Centers the object horizontally, without changing its size. Horizontal alignment: Center aligned horizontally |
| 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 fully fill its container. Fill horizontally |
| Center |
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. Centers the object horizontally, without changing its size. |
| Fill |
Grow the horizontal and vertical size of the object if needed so it completely fills it container. This is the default. When necessary, increase the cross-portrait size of the object to fully fill its container. |
| Clip_vertical |
Additional option that can is set to has 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 N Either clips both edges.
Additional options for cutting the contents of the top and/or bottom of the object according to the edges of the container. Clipping is based on its vertical alignment settings: When the top is aligned, the bottom is clipped, the top is clipped at the bottom, and the top and bottom are cut apart.
Crop vertically |
| Clip_horizontal |
Additional option that can is set to has 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.
An additional option to cut the contents of the left and/or right of the object according to the edges of the container. Clipping is based on its horizontal alignment settings: Cut to the right when left aligned, cut to the left when aligned to the right, and cut to the left and right.
Horizontal Orientation Clipping |
The following example
View Plain
- <?xmlversion= "1.0" encoding= "Utf-8"?>
- <linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
- android:orientation= "Vertical"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- >
- <textview
- Android:layout_width= "100dip"
- android:layout_height= "100dip"
- Android:layout_gravity= "Bottom|center_horizontal"
- Android:gravity= "Center|bottom"
- Android:background= "#00FF00"
- android:text= "@string/textview"
- />
- <button
- Android:layout_width= "100dip"
- android:layout_height= "100dip"
- Android:layout_gravity= "Bottom|left"
- Android:gravity= "Left|top"
- Android:background= "#FF0000"
- android:text= "@string/button"
- />
- </LinearLayout>
Note that TextView is not displayed in the center of the interface as we set the Android:layout_gravity property, and the button is not displayed at the bottom left of the interface. This is because we have set the Android:orientation property of LinearLayout to "vertical". For LinearLayout if the android:orientation= "vertical" is set, then the Android:layout_gravity setting is only in the horizontal direction, as the TextView appears in the horizontal center of the screen, The button appears at the far left of the horizontal direction, and if you set android:orientation= "Horizontal", the Android:layout_gravity property only takes effect in the vertical direction.
Gravity and Layout_gravity of Android layout properties