Android application realizes vertical and horizontal center layout with LinearLayout

  • 2021-07-01 08:11:27
  • OfStack

First of all, the center 1 under LinearLayout layout is like this:
(Note: In the android: layout_width= "fill_parent" android: layout_height= "fill_parent" attribute, if it is centered horizontally, it occupies at least full screen in width; if it is centered vertically, it occupies full screen in height.)


<LinearLayout 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:gravity="center|center_horizontal|center_vertical" > 
  //  Above gravity Parameters of the property: center For the center, center_horizontal Is horizontally centered, center_vertical Is vertically centered  
  <Button  
    android:id="@+id/Binding_button"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text=" Associate a new account " /> 
</LinearLayout>  

To achieve vertical and horizontal centering, several basic parameters are worth noting:
1. android: orientation = "vertical" indicates that the elements under this layout are arranged vertically;
2. android: layout_gravity= "center_horizontal" means that the layout is horizontally centered in the parent layout, at which time its parent layout must have android: orientation= "vertical" attribute;
3. android: layout_gravity= "center_vertical" means that the layout is vertically centered in the parent layout. At this time, its parent layout must be set to android: orientation= "horizontal" attribute (default is this attribute), and its height should be set to android: layout_height= "fill_parent" attribute;
4. android: gravity= "center_horizontal" means that the element level under this layout is centered;

Linear layout Vertical horizontal center layout file example:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal" 
    > 
 
    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_gravity="center_vertical" 
      > 
 
      <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
 
        <TextView 
          android:layout_width="100dp" 
          android:layout_height="wrap_content" 
          android:gravity="right" 
          android:text=" User name " /> 
 
        <EditText 
          android:layout_width="300dp" 
          android:layout_height="wrap_content" /> 
      </LinearLayout> 
 
      <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
 
        <TextView 
          android:layout_width="100dp" 
          android:layout_height="wrap_content" 
          android:gravity="right" 
          android:text="Email" /> 
 
        <EditText 
          android:layout_width="300dp" 
          android:layout_height="wrap_content" /> 
      </LinearLayout> 
    </LinearLayout> 
  </LinearLayout> 
</LinearLayout> 


Related articles: