LinearLayout:layout_gravity =“bottom”在水平LinearLayout上不起作用

好的,首先,我search了所有的互联网,但没有人有类似的问题。 所以,我想要的是有3个textViews,底部与屏幕alignment,并具有相同的宽度。 这是一张表示我想要的图像:

在这里输入图像说明

这是我的代码:

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <TextView android:text="@string/help_1" android:layout_weight="0.33" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/mynicebg1" android:layout_gravity="bottom"/> <TextView android:text="@string/help_2" android:layout_weight="0.33" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/mynicebg2" android:layout_gravity="bottom"/> <TextView android:text="@string/help_3" android:layout_weight="0.33" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/mynicebg3" android:layout_gravity="bottom"/> </LinearLayout> </RelativeLayout> 

那么,它的作品,当3个textViews具有相同的高度,但是当他们的大小不同,我得到以下结果:

问题

另一个奇怪的行为是,当我把最大文本的layout_gravity设置为“center-vertical”时,我得到以下结果:

第一个解决方法

所以显然,我疯了,尝试了与中心垂直的另一种组合,但没有任何工作,因为我最初想:

绝望的解决办法

那么,如何解决这个问题的任何提示?

正确答案

所有其他答案都是错误的。 重要的一点:

  1. 你不需要RelativeLayout 。 你可以用LinearLayout来做到这一点。
  2. (不重要,但我猜你不知道)你的权重不需要总和为1,你可以将它们全部设置为任何相等的值(例如1)。
  3. 关键的是你需要android:baselineAligned="false" 。 实际上我只是通过查看LinearLayout源代码才发现这一点。 这是在文档中,但他们没有提到,它默认情况下!

无论如何,这里是代码:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:baselineAligned="false"> <TextView android:text="dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#eeffee" android:layout_gravity="bottom"/> <TextView android:text="asd asd asd asd asd asd asd asd asd asd" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#eeeeff" android:layout_gravity="bottom"/> <TextView android:text="qweoiu qweoiuqwe oiqwe qwoeiu qweoiu qweoiuq weoiuqw eoiquw eoiqwue oqiweu qowieu qowieu qoiweu qowieu qowieu qowieu qowieu qoiweu qowieu qoiwue " android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffeeee" android:layout_gravity="bottom"/> </LinearLayout> 

它看起来如何:

良好的线性布局

好。 所以我有同样的问题,但切换button,而不是文字视图。 出于某种原因,如果LinearLayout(水平)中的某个元素与布局中的其余视图具有不同的高度,并且被设置为具有与其他视图相同的重力,则重力会被有效“忽略”。

我设法通过将每个视图包装在RelativeLayout中,并将android:gravity设置为相对布局而不是android:layout_gravity来实现所需的行为。 我也将android:layout_weight从button移动到相对布局。

所以,而不是有:

 <LinearLayout ... > <ToggleButton ... android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:layout_gravity="bottom"/> <ToggleButton ... android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:layout_gravity="bottom"/> <ToggleButton ... android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1" android:layout_gravity="bottom"/> </LinearLayout> 

这给出了与报告相同的问题,我反而做了:

 <LinearLayout ... > <RelativeLayout ... android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="bottom" > <ToggleButton ... android:layout_height="wrap_content" android:layout_width="fill_parent" /> <RelativeLayout ... android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="bottom" > <ToggleButton ... android:layout_height="wrap_content" android:layout_width="fill_parent" /> <RelativeLayout ... android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="bottom" > <ToggleButton ... android:layout_height="wrap_content" android:layout_width="fill_parent" /> </LinearLayout> 

**编辑:

如果这不起作用,请添加Timmmmm在下面的答案中提到的baselinealigned标志。 这是一个更好的方法。

用这个

EDITED布局:好吧,我编辑它,还添加了颜色和重力让底部的文字浏览有相同的高度和宽度,并在底部和每个视图的中心文字。

  <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:gravity="fill_vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:layout_gravity="fill_vertical" android:gravity="bottom|center" android:text="text1 jkjhh jhguk jvugy v ghjv kjhvygvusdioc jgytuayhashg hgyff" android:textColor="#000000" android:background="#FFFF00" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:layout_gravity="fill_vertical" android:gravity="bottom|center" android:text="t2" android:textColor="#000000" android:background="#FF0000" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:layout_gravity="fill_vertical" android:gravity="bottom|center" android:text="This is a long text. This is a long text. This is a long text. This is a long text.This is a long text. This is a long text. This is a long text." android:textColor="#000000" android:background="#00FF00" /> </LinearLayout> 

它应该做你正在问什么。

它在RelativeLayout中使用LinearLayout,但有时需要将它们嵌套以获得我们想要的。 在相对布局中添加任何您可能需要的视图,只要您的RelativeLayout中的最后一个子项是此LinearLayout,就会始终将其文本放在底部。

如果你可以使用RelativeLayout而不是Linear,那么这个技巧就可以实现,我想:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/LinearLayout1" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello" android:id="@+id/TextView1" android:layout_above="@+id/TextView2" android:layout_alignLeft="@+id/TextView2"></TextView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello" android:id="@+id/TextView2" android:layout_alignParentBottom="true"> </TextView> </RelativeLayout> 

如果您要将所有三个子视图的高度设置为相同,请将高度更改为“0”,将LinearLayout的android:weightSum设置为3,并将每个视图的android:layout_weight设置为1。

与Timmm的答案相同,但对于每个TextView ,您还可以使用android:gravity="bottom"作为LinearLayout属性,而不是android:layout_gravity="bottom"

喜欢这个:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="bottom" android:baselineAligned="false"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="your text 1......" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="your text 2......" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="your text 3......" android:layout_weight="1"/> </LinearLayout>