【问题标题】:RelativeLayout is taking fullscreen for wrap_contentRelativeLayout 正在全屏显示 wrap_content
【发布时间】:2011-06-26 19:34:00
【问题描述】:

当没有元素是layout_height="fill_parent" 时,为什么FOOBARZ 一直放在底部,换句话说,所有元素都是wrap_content 的高度?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/feed_u"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_marginLeft="5dip"
        android:scaleType="centerCrop"
        android:drawableTop="@android:drawable/presence_online"
        android:text="U" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/feed_u">
        <ImageView
            android:id="@+id/feed_h"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/btn_minus" />
        <ImageView
            android:id="@+id/feed_ha"
            android:layout_toLeftOf="@id/feed_h"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/btn_plus" />
        <TextView
            android:id="@+id/feed_t"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title">
        </TextView>
        <TextView
            android:id="@+id/feed_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Band"
            android:layout_below="@id/feed_t">
        </TextView>
        <TextView
            android:id="@+id/feed_s"
            android:layout_below="@id/feed_a"
            android:text="S"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </TextView>
        <TextView
            android:id="@+id/feed_tm"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="FOOBARZ"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </TextView>

    </RelativeLayout>
</RelativeLayout>

【问题讨论】:

  • 使用 hierarchyviewer 或 Eclipse 中的 Hierarchy View 透视图来确定哪里出了问题:developer.android.com/guide/developing/debugging/…
  • eclipse里面的hierarchy view透视图在哪里?哪个屏幕?
  • Window > Open Perspective > Other... -- 虽然我认为你已经有了答案,感谢 @alextc。
  • @CommonsWare 我找到了,但是它是空白的,我怎样才能真正将布局加载到其中??

标签: android android-layout


【解决方案1】:

来自RelativeLayout 文档:

类概述

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

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

Class documentation

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

【讨论】:

  • 所以我删除了 align_parentBottom 并且布局现在只有屏幕应该的 1/10,我怎样才能将 FOOBARZ 放在它的底部?
  • 只要看一眼,我会说:获取 FOOBARZ 文本视图,将其移动到外部 RelativeLayout 并设置 android:layout_below="@id/feed_u"。我不确定这是否是你想要的强硬。
  • 您是否尝试为相对布局分配固定高度(例如android:layout_height="200dp"。这将允许您使用android:layout_alignParentBottom="true"
  • 请注意,使用大型可绘制对象作为背景时也会发生这种情况。要绕过它,请将主要内容拉出到子布局中,并将背景设为其后面的兄弟&lt;ImageView&gt;
  • 我注意到孩子们也不允许使用layout_height of match_parent
【解决方案2】:

对于那些像我一样寻求解决方案的人,您可以使用FrameLayout 而不是RelativeLayout

然后您可以将预期对象的重力设置为右下角,如下所示

<TextView
    android:layout_gravity="bottom|right"
    android:text="FOOBARZ"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
</TextView>

【讨论】:

  • 使用 FrameLayout 是很好的解决方案。如果此布局中有更多组件,则必须设置边距以避免重叠。
【解决方案3】:

您已将 RelativeLayout 设置为 "wrap_content" 和 TextView 到android:layout_alignParentBottom="true",所以它会自动尝试将 RelativeLayout 拉伸到底部。不要将此类依赖项与相对布局一起使用,因为它可以算作“循环依赖项”。

来自the docs for RelativeLayout

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

尝试将 TextView 与父级 RelativeLayout 以外的其他内容对齐,但也要注意这个问题:
Circular dependencies, need some help with exact code

或者,尝试添加更复杂的内部布局。

【讨论】:

  • 顺便说一句,我认为在运行时 - 您的代码会按您的意愿显示,即使使用“android:layout_alignParentBottom="true"”。看来,Android 在运行时对此案例进行了一些额外的解析并正确显示。您应该尝试运行您的应用程序。
【解决方案4】:

不要对子视图使用 alight_Parent 类型属性

您可以使用框架布局而不是具有各自重力的RelativeLayout

    <FrameLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
     <TextView
        android:layout_gravity="bottom|right"
        android:text="Hello "
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

    </TextView>

</FrameLayout>

【讨论】:

    【解决方案5】:

    很好的答案。现在,如果您没有 layout_alignParentBottom="true" 并且仍然遇到此问题,请注意 android:background="@drawable/bkgnd" 其中 bkgnd 是一个大问题。

    【讨论】:

      【解决方案6】:

      我不确定为什么尚未发布完成此任务的简洁明了的方法。此高性能解决方案适用于任何已知高度的 View MyView。

      将您的 RelativeLayout 与高度 wrap_content 包装在 FrameLayout 中:

      <!-- width here should constrain RelativeLayout -->
      <FrameLayout 
           android:layout_width="@dimen/my_layout_width"
           android:layout_height="wrap_content">
      
           <RelativeLayout  
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
      
           <MyView
              ...
              android:layout_gravity="bottom" />
      </FrameLayout>
      

      请注意,FrameLayout 底部的视图将位于您的 RelativeLayout 内容之上,因此您需要在该布局的底部添加填充以适应它。如果您希望该视图是可变高度,您可以继承 FrameLayout 以根据测量的视图高度在代码中添加填充,或者如果您不担心性能,则只需将 FrameLayout 更改为垂直 LinearLayout,即它不是列表视图项,或者视图相对较轻。

      【讨论】:

        【解决方案7】:

        FrameLayout 通常适用于将不同的视图放在一起(其中最近的子视图位于前一个子视图的顶部)。在您的情况下,您希望将视图彼此相邻放置(上、下、开始、结束),所以我认为 ConstrainLayout 更合适,因为它正是它的作用。

        RelativeLayout 不同,您可以将ConstrainLayout 宽度设置为wrap_content,并且仍然可以根据需要排列其子视图,例如,而不是

        android:layout_alignParentTop="true"
        

        你可以使用

        grid:layout_constraintTop_toTopOf="parent" 
        

        而不是

        android:layout_alignParentBottom="true"
        

        你可以使用

        grid:layout_constraintBottom_toBottomOf="parent"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多