【问题标题】:RelativeLayout takes all its space, even though I've set its height to "wrap_content"RelativeLayout 占用了所有空间,即使我已将其高度设置为“wrap_content”
【发布时间】:2014-09-09 09:50:08
【问题描述】:

背景

我在 FrameLayout/RelativeLayout 中有一个 RelativeLayout(对我来说没关系),它应该在屏幕的底部(就像一个工具栏),并且应该在其中包含一些视图。

它的高度设置为“wrap_content”,它的子视图也是如此。

此布局的子视图是:左侧是 textView,右侧是带有几个按钮的 Horizo​​ntal LinearLayout。

问题

似乎无论我做什么,textview 都会导致 RelativeLayout 占据整个空间,而不仅仅是它的孩子。

代码

这是导致此问题的最小 XML 内容。我已经删除了额外的东西(LinearLayout 及其子项,以及一些无关紧要的属性),因为如果我删除 TextView,它们不会导致这个问题:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000" >

    <!-- Here I've put some views that don't have any relation with the views below, so it doesn't have anything with do with the problem -->

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/vertical_gradient_transparent_to_black" >

        <!-- Here I've put a LinearLayout that doesn't cause the problem, so I've removed it for simplicity-->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>

</RelativeLayout>

我尝试了许多可能的属性,还尝试添加其他布局以尝试“欺骗”RelativeLayout,但都没有成功。

问题

为什么会发生?

一个可行的解决方案是使用 Horizo​​ntal LinearLayout (带有 TextView 的权重)而不是 RelativeLayout ,但我仍然想知道为什么我不能使用 RelativeLayout 以及它发生的原因。还有如何在仍然使用 RelativeLayout 的同时修复它。

【问题讨论】:

  • 可能是因为android:layout_alignParentBottom="true"
  • 如果你从 TextView 中删除 android:layout_alignParentBottom="true"/&gt; 它应该可以工作..
  • @user3231871 但是如果我删除它,textView 将位于上部区域,而我希望它位于底部。

标签: android android-layout alignment textview android-relativelayout


【解决方案1】:

来自RelativeLayout 文档:

类概述

一种布局,其中子级的位置可以相对于彼此或与父级进行描述。

请注意,RelativeLayout 的大小与其子项的位置之间不能存在循环依赖关系。 例如,您不能拥有高度设置为 WRAP_CONTENT 且子级设置为 ALIGN_PARENT_BOTTOM 的 RelativeLayout

Class documentation

这正是你的情况。 RelativeLayout 做不到。

【讨论】:

  • 哇,这真是个不错的侦探!当然,我们都读过这个警告,但没有意识到犯这个错误是多么容易。
  • 这是一个很好的答案,应该被接受!
【解决方案2】:

我之前也遇到过同样的问题,这可能是android:layout_alignParentBottom属性引起的。也许你可以找到另一种方法来达到你想要的效果。请参阅this 了解更多信息。

【讨论】:

  • 我找到了方法 - 使用 LinearLayout 而不是 RelativeLayout。但问题是我只是想知道它为什么会发生,并且可能在不使用 LinearLayout 的情况下修复它。
  • 那我推荐你阅读RelativeLayout的源码
  • 为什么?不能使用RelativeLayout 留在底部,根据需要只取其子项的高度,而不是只取所需的空间吗?
【解决方案3】:

这是某种奇怪的问题。也许其他人有解释。

当我删除该行时,我发现它工作正常

android:layout_alignParentBottom="true"/>

当我尝试这样的方法时它也有效

<TextView
    android:layout_width="match_parent"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"/>

【讨论】:

  • 是的,但我需要它在底部。另外,我的问题是它为什么会发生,以及如何在使用RelativeLayout 时解决它。我已经更新了这个问题以便更多地解释它。
猜你喜欢
  • 2018-03-22
  • 1970-01-01
  • 2011-08-23
  • 2018-03-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多