I'm tyring to align linearlayout's vertical center which shows following pic (skycolor border) to delete button's vertical center.
我是tyring来对齐线性布局的垂直中心,它显示如下的图片(skycolor边框)来删除按钮的垂直中心。
so I set gravity of id:groupNumbers to center_vertical.
因此,我将id:groupNumbers设置为center_vertical。
but no changed.
但是没有改变。
How to align id:groupNumbers to buttons's center?
如何对齐id:groupNumbers到按钮的中心?
<?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>
5 个解决方案
#1
101
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.
希望这个有帮助。
#2
26
use android:layout_gravity
instead of android:gravity
使用android:layout_gravity而不是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.
android:重力设置视图内容的重力。android:layout_gravity设置父视图或布局的重力。
#3
6
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.
使用layout_gravity替代重力。layout_gravity告诉父节点它应该位于何处,重力告诉子节点它们应该位于何处。
<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">
#4
4
For me, I have fixed the problem using android:layout_centerVertical="true"
in a parent RelativeLayout
:
对我来说,我已经用android解决了这个问题:layout_centerVertical=“true”,在一个父类相对布局中:
<RelativeLayout ... >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true">
</RelativeLayout>
#5
2
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>
#1
101
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.
希望这个有帮助。
#2
26
use android:layout_gravity
instead of android:gravity
使用android:layout_gravity而不是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.
android:重力设置视图内容的重力。android:layout_gravity设置父视图或布局的重力。
#3
6
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.
使用layout_gravity替代重力。layout_gravity告诉父节点它应该位于何处,重力告诉子节点它们应该位于何处。
<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">
#4
4
For me, I have fixed the problem using android:layout_centerVertical="true"
in a parent RelativeLayout
:
对我来说,我已经用android解决了这个问题:layout_centerVertical=“true”,在一个父类相对布局中:
<RelativeLayout ... >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true">
</RelativeLayout>
#5
2
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>