Android: gravity and android: layout_gravity,
Android: gravity defines the display position of the content of the current element or the child element (subordinate element) contained in the current element.
Android: layout_gravity defines the display position of the current element in the parent element.
Example 1
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000" android:text="Text-01" android:textColor="@android:color/white" android:textSize="50sp" android:layout_marginTop="20dip" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ff00" android:text="Text-02" android:textColor="@android:color/white" android:textSize="50sp" android:layout_marginTop="20dip" android:gravity="center" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0000ff" android:text="Text-03" android:textColor="@android:color/white" android:textSize="50sp" android:layout_marginTop="20dip" android:layout_gravity="center" /></LinearLayout>
Display Effect:
The default alignment of TextView content is "Left alignment", for example, TextView with a red background;
After the android: gravity = "center" attribute is set, the TextView content is displayed in the center, such as the TextView with a green background;
Setting the android: layout_gravity = "center" attribute does not change the TextView content alignment mode, such as the TextView of the blue background.
Example 2
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ android: color/darker_gray" android: orientation = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "150dip" android: background = "#00ff00" android: orientation = "vertical"> <TextView android: layout_width = "Wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "# ff0000" android: text = "Text-01" android: textColor = "@ android: color/white" android: textSize = "20sp"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "# ff0000" android: text = "Text-02" android: textColor = "@ android: Color/white "android: textSize =" 20sp "/> </LinearLayout> <! -- Android: gravity = "center" indicates that all the child elements (two textviews) contained in LinearLayout are displayed in the center of the LinearLayout range --> <LinearLayout android: layout_width = "match_parent" android: layout_height = "150dip" android: background = "#00ff00" android: orientation = "vertical" android: layout_marginTop = "10dip" android: gravity = "center"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20di P "android: background =" # ff0000 "android: text =" Text-03 "android: textColor =" @ android: color/white "android: textSize = "20sp"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "# ff0000" android: text = "Text-04" android: textColor = "@ android: color/white" android: textSize = "20sp"/> </LinearLayout> <LinearLayout android: lay Out_width = "match_parent" android: layout_height = "150dip" android: background = "#00ff00" android: orientation = "vertical" android: layout_marginTop = "10dip"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "# ff0000" android: text = "Text-05" android: textColor = "@ android: color/white" android: textSize = "20sp"/> <! -- Android: layout_gravity = "center_horizontal" indicates that the current TextView is horizontally centered in the parent element (LinearLayout) --> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "# ff0000" android: text = "Text-06" android: textColor = "@ android: color/white "android: textSize =" 20sp "android: layout_gravity =" center_horizontal "/> </LinearLayout>
Display Effect:
After the android: gravity = "center" attribute is set in the second LinearLayout group, the two textviews of its subordinates are displayed in the center of the parent element (LinearLayout;
After the android: layout_gravity = "center_horizontal" attribute is set in the second TextView (Text-06) in the third LinearLayout group, the TextView is horizontally centered in its parent element (LinearLayout.