简体   繁体   English

线性布局:layout_gravity="bottom" 不适用于水平线性布局

[英]LinearLayout: layout_gravity="bottom" not working on Horizontal LinearLayout

Ok, First of all, I searched all the internet, but nobody has a similar problem like this.好的,首先,我搜索了所有互联网,但没有人有类似的问题。 So, all I want is to have 3 textViews, bottom aligned with the screen and with the same width.所以,我想要的只是有 3 个文本视图,底部与屏幕对齐并具有相同的宽度。 Here is an image representing what I want:这是代表我想要的图像:

在此处输入图像描述

And here is my code:这是我的代码:

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

Well, it works when the 3 textViews have the same height, but when their size differ, I get the following result:好吧,当 3 个 textViews 具有相同的高度时它可以工作,但是当它们的大小不同时,我得到以下结果:

问题

Another strange behavior, is that when I set the layout_gravity of the biggest text to "center-vertical", I get the following result:另一个奇怪的行为是,当我将最大文本的 layout_gravity 设置为“center-vertical”时,我得到以下结果:

第一个解决方法

So obviously, I went crazy and tried another combinations with center-vertical, but nothing worked as I wanted initially:所以很明显,我发疯了,尝试了另一种与 center-vertical 的组合,但最初没有任何效果:

绝望的解决方法

So, any tips on how to solve this?那么,关于如何解决这个问题的任何提示?

The Correct Answer正确答案

All the other answers are wrong.所有其他答案都是错误的。 The important points:要点:

  1. You don't need RelativeLayout .您不需要RelativeLayout You can do this with just a LinearLayout .你可以只用一个LinearLayout来做到这一点。
  2. (Not critical but I guess you didn't know) Your weights don't need to sum to 1, you can just set them all to any equal value (eg 1). (不重要,但我猜你不知道)你的权重不需要总和为 1,你可以将它们全部设置为任何相等的值(例如 1)。
  3. The critical thing is you need android:baselineAligned="false" .关键是你需要android:baselineAligned="false" I actually only found this by looking through the LinearLayout source.实际上,我只是通过查看LinearLayout源代码才发现这一点。 It is in the docs but they don't mention that it is on by default!它在文档中,但他们没有提到默认情况下它是打开的!

Anyway, here is the code:无论如何,这是代码:

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

And how it looks:以及它的外观:

良好的线性布局

Ok.好的。 So I had the same issue, but with toggle buttons instead of text views.所以我遇到了同样的问题,但使用的是切换按钮而不是文本视图。 For some reason, if one of the elements in the LinearLayout(Horizontal) has a different height than the rest of the views in the layout and is set to have the same gravity as the others, the gravity is effectively "ignored".出于某种原因,如果 LinearLayout(Horizontal) 中的元素之一具有与布局中视图的 rest 不同的高度,并且设置为与其他元素具有相同的重力,则重力实际上被“忽略”。

I managed to have the desired behavior by wrapping each view inside a RelativeLayout and set the android:gravity on the relative layout instead of android:layout_gravity on each button.我通过将每个视图包装在 RelativeLayout 中并在相对布局上设置 android:gravity 而不是每个按钮上的 android:layout_gravity 来设法获得所需的行为。 I also moved the android:layout_weight from the button to the relative layout.我还将 android:layout_weight 从按钮移动到相对布局。

So instead of having:因此,而不是:

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

Which gives the same problem as reported, I instead did:这给出了与报告相同的问题,我改为:

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

Same as Timmm's answer, but you also can use android:gravity="bottom" for LinearLayout attribute instead of android:layout_gravity="bottom" for each of TextView .与 Timmm 的答案相同,但您也可以将android:gravity="bottom"用于LinearLayout属性,而不是android:layout_gravity="bottom"用于每个TextView

Like this:像这样:

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

**EDIT: **编辑:

If this doesn't do everything, add the baselinealigned flag as mentioned in one of the answers below by Timmmmm.如果这不能解决所有问题,请添加下面 Timmmmm 的答案之一中提到的基线对齐标志。 That is a better way.这是一个更好的方法。

Use This用这个

EDITED LAYOUT: Ok I edited it and also added colors and gravity to let the textviews at the bottom have equal height and width and also aligh the text at the bottom and in the center of each view.编辑布局:好的,我编辑了它,还添加了 colors 和重力,让底部的文本视图具有相同的高度和宽度,并且在每个视图的底部和中心对齐文本。

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

It should do exactly what you asked.它应该完全按照您的要求进行。

It uses a LinearLayout inside a RelativeLayout but sometimes it is required to nest them to get what we want.它在 RelativeLayout 中使用 LinearLayout,但有时需要嵌套它们以获得我们想要的。 Add any more views you might want in the relative layout and you will always have your texts at the bottom as long as the last child in your RelativeLayout is this LinearLayout as shown above.在相对布局中添加您可能需要的任何更多视图,只要您的 RelativeLayout 中的最后一个子项是如上所示的此 LinearLayout,您将始终将文本放在底部。

A much easier solution would be to use the < Space > tag:一个更简单的解决方案是使用< Space >标签:

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

If you're ok with a RelativeLayout instead of Linear, this will do the trick, I guess:如果您对 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>

If you're trying to make all three child views the same height, then change height to "0", set the android:weightSum of the LinearLayout to 3, and the set the android:layout_weight of each view to 1.如果您要使所有三个子视图的高度相同,则将高度更改为“0”,将 LinearLayout 的 android:weightSum 设置为 3,并将每个视图的 android:layout_weight 设置为 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM