This article Reprinted from: http://blog.csdn.net/dekunchenivan/article/details/6718678 #
The Chinese meaning of gravity is "Center of Gravity", indicating the horizontal and vertical parking position of the view.
Android: gravity: The view control is used to set the position where the view content should be displayed. The default value is left.
Android: layout_gravity: Relative to the parent element that contains the modified element, set the location of the element in the parent element. For example, textview: Android: layout_gravity indicates the location of the textview on the interface. Android: gravity indicates the position of textview text in textview. The default value is left. basic option value:
| 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 |
Example 1
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="100dip" android:layout_height="100dip" android:layout_gravity="bottom|center_horizontal" android:background="#00FF00" android:gravity="center|bottom" android:text="textview" /> <Button android:layout_width="100dip" android:layout_height="100dip" android:layout_gravity="bottom|left" android:background="#FF0000" android:gravity="left|top" android:text="button" /></LinearLayout>
Display result
Note:: Textview is not displayed in the center of the square under the interface as we set the Android: layout_gravity attribute, and the button is not displayed in the lower left of the interface. This is because the Android: Orientation attribute of linearlayout is set to "vertical ". For linearlayout, if Android: Orientation = "vertical" is set, the setting of Android: layout_gravity takes effect only in the horizontal direction. For example, textview is displayed in the center of the screen horizontally, the button is displayed on the far left of the horizontal direction. If Android: Orientation = "horizontal" is set, the Android: layout_gravity attribute takes effect only in the vertical direction! Post this comment to Weibo