How to align LinearLayout at the center of its parent?

前端 未结 14 2024
北荒
北荒 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 10:05

    Try this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
                <TableRow >
                <LinearLayout 
                android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal" 
                        android:layout_gravity="center_horizontal" >
                        <TextView
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:text="1"
                        android:textAppearance="?android:attr/textAppearanceLarge" />
    
                         <TextView
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:text="2"
                        android:textAppearance="?android:attr/textAppearanceLarge" />
                 </LinearLayout>
               </TableRow>
            </TableLayout>
        </FrameLayout>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 10:05

    for ImageView or others views who layout-gravity or gravity does not exist use : android:layout_centerInParent="true" in the child (I had a relative layout with an ImageView as a child).

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

    I have faced the same situation while designing custom notification. I have tried with the following attribute set with true.

     android:layout_centerInParent.
    
    0 讨论(0)
  • 2020-12-04 10:10

    The problem is that the root's (the main layout you store the other elements) gravity is not set. If you change it to center the other elements' gravity must work just fine.

    0 讨论(0)
  • 2020-12-04 10:12
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout 
                android:orientation="horizontal"
                android:layout_centerHorizontal="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:text="1"
                     android:textAppearance="?android:attr/textAppearanceLarge" />
    
               <TextView
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:text="2"
                     android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>
    </RelativeLayout>
    

    consider wrapping relativeLayout over LinearLayout. android:layout_centerHorizontal="true" will position the view center horizontal.

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

    add layout_gravity="center" or "center_horizontal" to the parent layout.

    On a side note, your LinearLayout inside your TableRow seems un-necessary, as a TableRow is already an horizontal LinearLayout.

    0 讨论(0)
提交回复
热议问题