How to align linearlayout to vertical center?

后端 未结 8 1760
你的背包
你的背包 2020-12-08 12:43

I\'m trying to align LinearLayout\'s vertical center which shows following pic (skycolor border) to delete button\'s vertical center.

so I set the gravity of id:grou

相关标签:
8条回答
  • 2020-12-08 13:25

    Use layout_gravity instead of gravity. layout_gravity tells the parent where it should be positioned, and gravity tells its child where they should be positioned.

    <LinearLayout
        android:id="@+id/groupNumbers"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
    0 讨论(0)
  • 2020-12-08 13:32

    use android:layout_gravity instead of android:gravity

    android:gravity sets the gravity of the content of the View its used on. android:layout_gravity sets the gravity of the View or Layout in its parent.

    0 讨论(0)
  • 2020-12-08 13:34

    For a box that appears in the center - horizontal & vertical - I got this to work with just one LinearLayout. The answer from Viswanath L was very helpful

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/layout_bg"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp">
    
        <TextView
            android:id="@+id/dialog_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:text="Error"
            android:textColor="#000" />
    
        <TextView
            android:id="@+id/message_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:text="Error-Message"
            android:textColor="#000" />
    
    
        <Button
            android:id="@+id/dialogButtonOK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/message_text"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:text="Ok" />
    
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-08 13:35

    You can change set orientation of linearlayout programmatically by:

    LinearLayout linearLayout =new linearLayout(this);//just to give the clarity
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    
    0 讨论(0)
  • 2020-12-08 13:36

    For me, I have fixed the problem using android:layout_centerVertical="true" in a parent RelativeLayout:

    <RelativeLayout ... >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerVertical="true">
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-08 13:36

    use RelativeLayout inside LinearLayout

    example:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Status"/>
            </RelativeLayout>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题