RelativeLayout正在全屏显示wrap_content

为什么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> 

RelativeLayout文档:

课程概述

一个布局,其中的孩子的位置可以描述为相互之间或父母。

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

类文档

这正是你的情况。 RelativeLayout不能这样做。

对于那些正在寻找解决scheme,像我一样,你可以使用FrameLayout而不是RelativeLayout

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

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

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

来自RelativeLayout的文档 :

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

尝试将TextView与父亲RelativeLayout以外的东西alignment,但要小心这个问题:
循环依赖关系,需要一些精确的代码的帮助

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

很好的答案。 现在,如果你没有layout_alignParentBottom="true" ,仍然得到这个问题,注意android:background="@drawable/bkgnd" ,其中bkgnd是一个biggie。

我不知道为什么完成这个干净明显的方式尚未公布。 该性能解决scheme适用于任何具有已知高度的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,也就是说不是列表视图项目,或视图相对轻量级。

不要使用子视图的alight_Parenttypes属性

您可以使用框架布局来代替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>