First distinguish between gravity and layout_gravity:
Gravity: The alignment of the inner element.
Layout_gravity: The alignment of this element with respect to the parent layout.
LinearLayout:
Centered relative to the parent layout, the LinearLayout setting in the parent layout: android:gravity= "center".
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></LinearLayout>
View Code
Centered horizontally relative to the parent layout, child layout settings: android:layout_gravity= "Center_horizontal", Parent layout settings: android:orientation= "Vertical", Android: Layout_width= "Match_parent".
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal"Android:text= "@string/hello_world" /></LinearLayout>
View Code
Centered vertically relative to the parent layout, child layout settings: android:layout_gravity= "Center_vertical", Parent layout settings:
android:orientation= "Horizontal", android:layout_height= "Match_parent".
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:text= "@string/hello_world" /></LinearLayout>
View CodeRelativelayout:
Centered relative to the parent layout, the Relativelayout setting in the parent layout: android:gravity= "center".
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></Relativelayout>
View Code
Centered relative to the parent layout, in child layout settings: android:layout_centerinparent= "true".
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"android:layout_centerinparent= "true" /></Relativelayout>
View Code
Centered horizontally and vertically relative to the parent layout corresponds to: Android:layout_centerhorizontal= "true", android:layout_centervertical= "true".
Centering problems with layouts