RelativeLayout正在全屏显示wrap_content

hun*_*erp 123 android android-layout

为什么FOOBARZ在没有元素的情况下一直在底部布局layout_height="fill_parent",所有元素都是高度的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>
Run Code Online (Sandbox Code Playgroud)

小智 259

来自RelativeLayoutdoc:

课程概述

可以相对于彼此或与父母相关地描述孩子的位置的布局.

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

课程文件

这正是你的情况.RelativeLayout无法做到这一点.

  • 所以我删除了align_parentBottom,布局现在只有屏幕的1/10,我怎么能把FOOBARZ放在这个底部呢? (3认同)
  • 只需简短看一下,我就会说:拿起FOOBARZ文本视图,将其移到外部RelativeLayout并设置android:layout_below =“ @ id / feed_u”。我不确定这是否是您想要的强项。 (2认同)

Muz*_*Muz 51

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

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

<TextView
    android:layout_gravity="bottom|right"
    android:text="FOOBARZ"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
</TextView>
Run Code Online (Sandbox Code Playgroud)


Dir*_*rol 21

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

来自RelativeLayout的文档:

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

尝试将TextView与父RelativeLayout之外的其他东西对齐,但也注意这个问题:
循环依赖,需要一些精确代码的帮助

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

  • 顺便说一句,我认为在运行时 - 你的代码会显示你想要的,即使是"android:layout_alignParentBottom ="true"".看来,Android在运行时对此案例进行了一些额外的解析并正确显示.你应该尝试运行你的应用程序. (2认同)