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 the text used to set the view itself should be displayed in the view where the default value is the 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 TextView text is in TextView, and the default value is the left
The following example
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns: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.
Transferred from: http://blog.csdn.net/dekunchenivan/article/details/6718678
Differences between the android:layout_gravity and Android:gravity attributes (RPM)