【问题标题】:LinearLayout: layout_gravity="bottom" not working on Horizontal LinearLayout线性布局:layout_gravity="bottom" 不适用于水平线性布局
【发布时间】:2011-07-04 19:32:49
【问题描述】:

好的,首先,我搜索了所有互联网,但没有人有类似的问题。所以,我想要的只是有 3 个文本视图,底部与屏幕对齐并具有相同的宽度。这是代表我想要的图像:

这是我的代码:

 <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”时,我得到以下结果:

显然,我发疯了并尝试了另一种与 center-vertical 的组合,但最初没有任何效果:

那么,有什么技巧可以解决这个问题吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    正确答案

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

    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>
    

    以及它的外观:

    【讨论】:

    • 一个非常简单的修复程序,它立即对我有用。我希望我在当前接受的答案之前阅读过这篇文章。
    • @Timmmm 我有自己的 TextView 实现扩展 TextView。而且您的“baseAligned”解决方案对我不起作用。请大家猜一猜?
    • 不知道。建议您查看LinearLayout 的代码以了解它如何使用baselineAligned 并检查您的代码在哪里覆盖了它的方法。你用的是baselineAligned 而不是baseAligned 对吧?
    【解决方案2】:

    好的。所以我遇到了同样的问题,但使用的是切换按钮而不是文本视图。出于某种原因,如果 LinearLayout(Horizo​​ntal) 中的一个元素的高度与布局中的其余视图不同,并且设置为与其他元素具有相同的重力,则重力实际上被“忽略”了。

    我设法通过将每个视图包装在 RelativeLayout 中并在相对布局上设置 android:gravity 而不是每个按钮上的 android:layout_gravity 来获得所需的行为。我还将 android:layout_weight 从按钮移动到相对布局。

    所以不要有:

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

    【讨论】:

    • 这是我最终必须做的才能修复它。我在 ScrollView 内的 LinearLayout 内有一个按钮(在另一个 LinearLayout 内)。在我将按钮放在另一个 LinearLayout 中之前,按钮不会与底部对齐
    【解决方案3】:

    **编辑:

    如果这不能解决所有问题,请添加下面 Timmmmm 的答案之一中提到的基线对齐标志。这是一个更好的方法。

    使用这个

    编辑布局: 好的,我对其进行了编辑,还添加了颜色和重力,以使底部的文本视图具有相同的高度和宽度,并在每个视图的底部和中心对齐文本。

        <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里面了,我省略了..不过还是谢谢..看看问题是,文本在底部,但它们没有对齐。
    • 我通过将重力设置为中心来测试它,正如我所展示的那样,它们运行良好。我可能不明白您是如何尝试对齐它们的。你能描述一下吗?
    • 您的文本必须多于一行。我的第一个文本有 5 行,第二个有 3 行,最后一个有 7 行。我试图在图像上表示它,但我无法提交应用程序的屏幕截图,因为我的雇主不喜欢它..
    • 哦,好的,在这种情况下,将每个 textview 的 layout_height 设置为 match_parent。我还更改了上面的代码。请检查它,看看它是否有效。
    • @Paulo Cesar:我已经编辑了代码以按照您的要求进行布局。我对其进行了测试,并为每个文本视图添加了颜色。如果这不是您想要的,请告诉我。
    【解决方案4】:

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

    像这样:

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

    【讨论】:

    • 感谢您的编辑,TheEsisia。我没有任何经验:)
    【解决方案5】:

    如果您可以使用 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>
    

    【讨论】:

    • 但是对于 RelativeLayout 我不能使用 android:layout_weight="0.33" 使所有的 textViews 大小相同。除非有更好的方法使它们具有相同的大小?
    • 在最坏的情况下,您可以使用宽度或高度的倾角值使它们达到相同的尺寸,但我不确定我是否在这里遇到相同尺寸的问题......
    • 指定值对我不起作用,我希望它们根据父宽度进行调整...
    【解决方案6】:

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

    【讨论】:

    • 哦,我不希望它们有相同的高度。我希望它们具有相同的宽度..
    • 您是要水平堆叠还是垂直堆叠?
    • 糟糕,我想将它们水平堆叠,我的问题标题错误。很抱歉造成混乱..
    • 然后设置LinearLayout的orientation为horizo​​ntal,weight_sum为3,对于每个子view,将view的高度设置为match_parent,宽度设置为0,layout_weight设置为1。
    • 好吧,我照你说的做了。现在 TextViews 已正确对齐,但它们都具有相同的高度。但我不希望他们有相同的高度......
    【解决方案7】:

    一个更简单的解决方案是使用 标签:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
          <Space
               android:layout_width="0dp"
               android:layout_height="1dp"
               android:layout_weight="1" />
    
          <TextView 
               android:text="Any Text"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#eeffee"/>
    
          <TextView 
               android:text="Any Text2"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#eeeeff"/>
    
    
          <TextView 
               android:text="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#ffeeee"/>
    
     </LinearLayout>
    

    【讨论】: