How to align LinearLayout at the center of its parent?

前端 未结 14 2023
北荒
北荒 2020-12-04 09:29

I\'ve looked through numerous similar questions here at SO, but nothing helps. I have a hierarchy of different nested layouts, all have android:layout_width=\"fill_par

相关标签:
14条回答
  • 2020-12-04 09:51

    @jksschneider explation is almost right. Make sure that you haven't set any gravity to parent layout, and then set layout_gravity="center" to your view or layout.

    0 讨论(0)
  • 2020-12-04 09:56

    These two attributes are commonly confused:

    • android:gravity sets the gravity of the content of the View it's used on.
    • android:layout_gravity sets the gravity of the View or Layout relative to its parent.

    So either put android:gravity="center" on the parent or android:layout_gravity="center" on the LinearLayout itself.

    I have caught myself a number of times mixing them up and wondering why things weren't centering properly...

    0 讨论(0)
  • 2020-12-04 09:59

    This worked for me.. adding empty view ..

    <LinearLayout
           android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
             >
    <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                />  
         <com.google.android.gms.ads.AdView
    
             android:id="@+id/adView"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
    
             android:layout_gravity="center_horizontal"
             ads:adSize="BANNER"
             ads:adUnitId="@string/banner_ad_unit_id" >
    
        </com.google.android.gms.ads.AdView>
        <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                />  
        </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 09:59

    Below is code to put your Linearlayout at bottom and put its content at center. You have to use RelativeLayout to set Layout at bottom.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#97611F"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="View Saved Searches"
            android:textSize="15dp"
            android:textColor="#fff"
            android:gravity="center_vertical"
            android:visibility="visible" />
        <TextView
            android:layout_width="30dp"
            android:layout_height="24dp"
            android:text="12"
            android:textSize="12dp"
            android:textColor="#fff"
            android:gravity="center_horizontal"
            android:padding="1dp"
            android:visibility="visible" />
      </LinearLayout>  
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-04 10:02

    this worked for me.

    <LinearLayout>
    .
    .
    .
    android:gravity="center"
    .
    .>
    
    <TextView
    android:layout_gravity = "center"
    />
    <Button
    android:layout_gravity="center"
    />
    
    </LinearLayout>
    

    so you're designing the Linear Layout to place all its contents(TextView and Button) in its center and then the TextView and Button are placed relative to the center of the Linear Layout

    0 讨论(0)
  • 2020-12-04 10:05

    Please try this in your linear layout

    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    
    0 讨论(0)
提交回复
热议问题