How to center the content inside a linear layout?

前端 未结 6 1985
无人共我
无人共我 2020-12-04 08:07

I\'m trying to center an ImageView inside a LinearLayout horizontally and vertically, but I just can\'t do it. The main reason why I\'m not using a

相关标签:
6条回答
  • 2020-12-04 08:45

    android:gravity handles the alignment of its children,

    android:layout_gravity handles the alignment of itself.

    So use one of these.

    <LinearLayout xmlns: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:background="#000"
        android:baselineAligned="false"
        android:gravity="center"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Main" >
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >
    
            <ImageView
                android:id="@+id/imageButton_speak"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_bg"
                android:src="@drawable/ic_speak" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >
    
            <ImageView
                android:id="@+id/imageButton_readtext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_bg"
                android:src="@drawable/ic_readtext" />
        </LinearLayout>
    
        ...
    </LinearLayout>
    

    or

    <LinearLayout xmlns: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:background="#000"
        android:baselineAligned="false"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Main" >
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1" >
    
            <ImageView
                android:id="@+id/imageButton_speak"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/image_bg"
                android:src="@drawable/ic_speak" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1" >
    
            <ImageView
                android:id="@+id/imageButton_readtext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/image_bg"
                android:src="@drawable/ic_readtext" />
        </LinearLayout>
    
        ...
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 08:50

    android:gravity can be used on a Layout to align its children.

    android:layout_gravity can be used on any view to align itself in its parent.

    NOTE: If self or children is not centering as expected, check if width/height is match_parent and change to something else

    0 讨论(0)
  • 2020-12-04 08:57

    Here's some sample code. 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 08:58

    android:layout_gravity is used for the layout itself

    Use android:gravity="center" for children of your LinearLayout

    So your code should be:

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1" >
    
    0 讨论(0)
  • 2020-12-04 08:58

    If you are looking for an answer in java code,

    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setGravity(Gravity.CENTER);
    // add children
    
    0 讨论(0)
  • 2020-12-04 09:02

    I tried solutions mentioned here but It didn't help me. I mind the solution is layout_width have to use wrap_content as value.

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1" >
    
    0 讨论(0)
提交回复
热议问题