LinearLayout:layout_gravity ="bottom"不在Horizo​​ntal LinearLayout上工作

Pau*_*sar 45 android

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

在此输入图像描述

这是我的代码:

 <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>
Run Code Online (Sandbox Code Playgroud)

好吧,当3个textView具有相同的高度时,它可以工作,但是当它们的大小不同时,我得到以下结果:

问题

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

第一个解决方法

显然,我疯了,尝试了另一种中心垂直组合,但最初没有任何效果:

绝望的解决方法

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

Tim*_*mmm 108

正确答案

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

  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>
Run Code Online (Sandbox Code Playgroud)

它看起来如何:

良好的线性布局