Android 如何将线性布局与垂直中心对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20185340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align linearlayout to vertical center?
提问by S.J. Lim
I'm trying to align LinearLayout's vertical center which shows following pic (skycolor border) to delete button's vertical center.
我正在尝试对齐显示以下图片(天空色边框)的 LinearLayout 的垂直中心以删除按钮的垂直中心。
so I set the gravity of id:groupNumbers to center_vertical.
所以我将 id:groupNumbers 的重力设置为 center_vertical。
but no changed.
但没有改变。
How to align id:groupNumbers to button'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>
回答by MysticMagic?
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.
希望这可以帮助。
回答by Viswanath Lekshmanan
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:gravity
设置其使用的视图内容的重力。
android:layout_gravity
在其父级中设置视图或布局的重力。
回答by Lawrence Choy
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
代替gravity
。layout_gravity
告诉父级它应该放置在哪里,并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">
回答by Mandy
For me, I have fixed the problem using android:layout_centerVertical="true"
in a parent RelativeLayout
:
对我来说,我已经解决了android:layout_centerVertical="true"
在父级中使用的问题RelativeLayout
:
<RelativeLayout ... >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true">
</RelativeLayout>
回答by Gene Bo
For a box that appears in the center - horizontal & vertical - I got this to work with just one LinearLayout. The answer from Viswanath Lwas very helpful
对于出现在中心的框 - 水平和垂直 - 我只用一个 LinearLayout 就可以使用它。Viswanath L的回答非常有帮助
<?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>
回答by Somwang Souksavatd
use RelativeLayout inside LinearLayout
在 LinearLayout 中使用 RelativeLayout
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>
回答by Udara Abeythilake
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
android:weightSum
对父 LinearLayout使用属性并赋予值 3。然后在子 LinearLayout 中android:layout_weight
分别使用2 和 1。然后在 First Chil LinearLayout 中使用android:layout_gravity="center_vertical"
. 如以下代码所示
<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>
回答by Bobby Sebastian
You can change set orientation of linearlayout programmatically by:
您可以通过以下方式以编程方式更改线性布局的设置方向:
LinearLayout linearLayout =new linearLayout(this);//just to give the clarity
linearLayout.setOrientation(LinearLayout.VERTICAL);