RelativeLayout 将全屏显示包装内容

为什么 FOOBARZ 的布局都在底部,而没有任何元素是 layout_height="fill_parent",换句话说,所有元素的高度都是 wra _ content? enter image description here

<?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>
53128 次浏览

来自 RelativeLayout的文档:

课程概览

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

请注意,RelativeLayout 的大小与其子元素的位置之间不能有循环依赖关系。例如,不能有高度设置为 WRAP _ CONTENT 的 RelativeLayout 和设置为 ALIGN _ PARENT _ BOTTOM 的子级

Class documentation

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

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

来自 相对布局的文档:

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

试着将你的 TextView 和其他东西对齐,而不是父 RelativeLayout,但是也要注意这个问题:
循环依赖项,需要一些精确代码的帮助

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

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

然后你可以设置重力的预期对象底部的权利如下

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

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

我不知道为什么干净利落的方法还没有公布出来。此高性能解决方案适用于任何具有已知高度的 ViewMyView。

在框架布局中用高度 wrap_content包裹你的 RelativeLayout:

<!-- 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 _ Parent 类型属性

您可以使用框架布局,而不是相对布局与各自的重力

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

FrameLayout通常用于将不同的视图一个放在另一个上面(最近的子视图放在前一个子视图的上面)。在您的例子中,您希望将视图一个挨着一个放置(上面、下面、开始、结束) ,所以我认为 ConstrainLayout更适合,因为它就是这样做的。

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

android:layout_alignParentTop="true"

你可以利用

grid:layout_constraintTop_toTopOf="parent"

而不是

android:layout_alignParentBottom="true"

你可以利用

grid:layout_constraintBottom_toBottomOf="parent"

不确定为什么这里的所有答案都建议使用 FrameLayout,它被设计为呈现 z 轴中的单个视图或视图层。OP 的问题是一系列垂直堆叠的视图,它们应该位于 LinearLayout中。