Ext.: http://blog.csdn.net/shakespeare001/article/details/7843460
1. First look at the difference between the use of android:layout_gravity and android:gravity.
android:gravity:
This is for the elements in the control, to control where the element is displayed in the control. For example, set the following two properties in a button control
Android:gravity= "left" and android:text= "submit", then the text "Submit" on the button will be on the right side of the button.
Android:layout_gravity:
This is for the control itself, which controls the position of the control in the parent control that contains the control. Similarly, when we set the android:layout_gravity= "left" property in the button control, it means that the button will be located at the right-hand side of the interface.
2. Attribute values:
The optional values for these two properties are:top, bottom, left, right, center_vertical, fill_vertical, Center_horizontal, Fill_horizontal, center,
Fill, clip_vertical.
A property can contain multiple values and requires the "|" Separate. It has the following meanings:
| Top |
Places the object at the top of its container without changing its size. |
| Bottom |
Place an object at the bottom of its container without changing its size |
| Left |
Places the object on the left side of its container without changing its size. |
| Right |
Place an object on the right side of its container without changing its size |
| Center_vertical |
Centers the object vertically without changing its size.
Vertical alignment: Center-aligned vertically. |
| Fill_vertical |
When necessary, increase the vertical size of the object to fully fill its container
Vertical fill |
| Center_horizontal |
Centers the object horizontally, without changing its size.
Horizontal alignment: Center aligned horizontally |
| Fill_horizontal |
When necessary, increase the horizontal size of the object to fully fill its container
Fill horizontally |
| Center |
Centers the object horizontally, without changing its size. |
| Fill |
When necessary, increase the cross-portrait size of the object to fully fill its container. |
| Clip_vertical |
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 |
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 |
We mainly look at the center_vertical and center_horizontal two attribute values, center_vertical refers to the object in the vertical direction of alignment, that is, in the direction from the top to the bottom of the selection of the middle position; center_ Horizontal refers to centering the object horizontally, that is, selecting the middle position in the left-to-right direction.
3. Special Circumstances
When we adopt the LinearLayout layout, we should pay attention to the following special cases:
(1) When android:orientation= "vertical", the android:layout_gravity only works horizontally, and the vertical setting does not work. That is: The left,right,center_horizontal is in force.
(2) when the android:orientation= "horizontal", android:layout_gravity only the vertical direction of the setting to function, the horizontal direction of the setting does not work. That is: The top,bottom,center_vertical is in force.
Here is an example: (This example is from: http://blog.csdn.net/dekunchenivan/article/details/6718678)
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_p Arent "android:layout_height=" fill_parent "> <textview Android:layout_widt H= "100dip" android:layout_height= "100dip" android:layout_gravity= "Bottom|center_horizontal" Android:gravity= "Center|bottom" android:background= "#00FF00" android:text= "@string/tex Tview "/> <button android:layout_width=" 100dip "android:layout _height= "100dip" android:layout_gravity= "Bottom|left" android:gravity= "Left|top" an Droid:background= "#FF0000" android:text= "@string/button"/> </LinearLayout>
Its effect
In TextView, we set up a
But the TextView is not shown in the center of the screen, indicating that only the Center_horizontal property is working, because we used the linearlayout layout,
and its
android:orientation= "Vertical"
Only the horizontal setting will work and the other directions will be invalidated. Similarly, the button is the same.
The difference between android:layout_gravity and android:gravity (RPM)