Android:gravity defines the display position of the current element content or the child elements (subordinate elements) contained within the current element.
Android:layout_gravity defines where the current element appears 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: Co Lor/darker_gray "android:orientation=" vertical "> <textview android:layout_width=" match_parent " android:layout_height= "Wrap_content" android:background= "#ff0000" android:text= "Text-01" Android:text Color= "@android: Color/white" android:textsize= "50sp" android:layout_margintop= "20dip"/> <t Extview 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 Andro Id:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:background= "#0000ff" android:text= "Text-03" android:t Extcolor= "@android: Color/white" android:textsize= "50sp" android:layout_margintop= "20dip" Android oid:layout_gravity= "Center"/></linearlayout>
Display effect:
TextView content default alignment is "left-aligned", such as the red background of the TextView;
After setting the android:gravity= "Center" property, the TextView content becomes centered, such as the textview of the green background;
Setting the android:layout_gravity= "Center" property does not change TextView content alignment, such as TextView on a 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: Co Lor/darker_gray "android:orientation=" vertical "> <linearlayout android:layout_width=" match_parent " android:layout_height= "150dip" android:background= "#00ff00" android:orientation= "vertical" > & Lt TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android : layout_margintop= "20dip" android:background= "#ff0000" android:text= "Text-01" Android:tex Tcolor= "@android: Color/white" android:textsize= "20sp"/> <textview android:layo Ut_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" means that LinearLayout contains all the child elements (2 TextView) in LinearLayout Centered within 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:layou t_height= "Wrap_content" android:layout_margintop= "20dip" android:background= "#ff0000" and roid: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= "@andro Id:color/white "android:textsize=" 20sp "/> </LinearLayout> <linearlayout android:layou T_width= "Match_parent" android:layout_height= "150dip" android:background= "#00ff00" Android:orientatio n= "vertical" android:layout_margintop= "10dip" > <textview android:layout_width= "Wra P_content "android:layout_height=" wrap_content "android:layout_margintop=" 20dip "Android: Background= "#ff0000" android:text= "Text-05" android:textcolor= "@android: Color/white" Android Oid:textsize= "20sp"/> <!--android:layout_gravity= "center_horizontal" means the current TextView in the parent element (Linear Layout) horizontally centered--<textview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margintop= "20dip" android:background= "#ff0000" android:text= "Text-06" an Droid:textcolor= "@android: Color/white" android:textsize= "20sp" android:layout_gravity= "C Enter_horizontal "/> </LinearLayout> </LinearLayout>
Display effect:
After the second set of LinearLayout set the android:gravity= "Center" property, the 2 TextView of its subordinates are centered in the parent element (LinearLayout);
After the second TextView (Text-06) in the third set of LinearLayout set the android:layout_gravity= "Center_horizontal" property, The TextView is centered horizontally in its parent element (LinearLayout).
The difference between android:gravity and android:layout_gravity