如何调整线性布局垂直中心?

我试图对齐 LinearLayout 的垂直中心,它显示下面的图片(天空彩色边框)删除按钮的垂直中心。

所以我把 id 的重力: groupNumber 设置为 center _ 竖直。

但是没有改变。

如何对齐 id: groupNumber 到按钮的中心?

enter image description here

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:id="@+id/groupNumbers"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_weight="0.7"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<TextView
android:id="@+id/txtSelected01"
android:text="00"
android:textSize="30dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>


<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<TextView
android:id="@+id/txtSelected02"
android:text="00"
android:textSize="30dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>




<Button
android:id="@+id/btn_deleteNum"
android:text="Delete"
android:textSize="20dp"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
206947 次浏览

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.

Change orientation and gravity in

<LinearLayout
android:id="@+id/groupNumbers"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_weight="0.7"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

to

android:orientation="vertical"
android:layout_gravity="center_vertical"

You are adding orientation: horizontal, so the layout will contain all elements in single horizontal line. Which won't allow you to get the element in center.

Hope this helps.

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">

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>

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>

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>

Use android:weightSum property to the parent LinearLayout and give value 3. Then in the children LinearLayout use android:layout_weight 2 and 1 respectively. Then in the First Chil LinearLayout use android:layout_gravity="center_vertical". As shown in the following code

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="@color/red"
android:textSize="@dimen/default_text_size"
/>
<TextView
android:id="@+id/status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="@color/red"
android:textSize="@dimen/default_text_size"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Delete"/>
</LinearLayout>
</LinearLayout>

You can change set orientation of linearlayout programmatically by:

LinearLayout linearLayout =new linearLayout(this);//just to give the clarity
linearLayout.setOrientation(LinearLayout.VERTICAL);