如何使一个 `View` 填满一个 `RelativeLayout`?

9

我有一个RelativeLayout,里面有一些东西,我想要一个背景视图来填充整个控件。这是我期望的代码:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000" >

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="1px"
                android:background="#fafafa" />

            <ImageView
                android:id="@+id/im"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_blank_profile" />

        .... and so on.

那样不起作用。它会填充宽度,但高度被设置为零。但是,如果我将layout_alignParentBottom更改为layout_alignBottom="@+id/im",那么它就会起作用(有点;这不是一个令人满意的解决方案,但至少它不再是0高度了)。

发生了什么?这是个bug吗?

2个回答

2

尝试将相对布局的layout_height属性设置为match_parent

如果从视图中删除所有 alignParent 属性,只使用 match_parent 来设置宽度和高度,则会发生什么?

Rolf

最终编辑?

小例子,类似于这样? 使用容器?当内部RelativeLayout的内容增长时,View会随之拉伸。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/relativeLayout1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_margin="1px"
            android:background="#fafafa" />

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/im"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_blank_profile" />
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

RelativeLayoutlayout_height 类型不会影响其任何子元素。我的意思是它已经是非零高度,所以子元素中的 match_parent 应该填充它。不幸的是,我已经尝试了你的第二个建议。没有成功。 - Timmmm
关于您的编辑:是的,那就是我的代码目前的样子,也是我所拥有的最好的代码,但这并不是我要寻找的解决方案。如果您将其他视图添加到“RelativeLayout”中,使其变得更大,则背景“View”将不再填充它。 - Timmmm
修改了我的帖子,现在我正在使用一个容器来获得你想要的行为。 - Rolf ツ
当然,我可以使用FrameLayout来包含背景视图和像你所做的那样的另一个RelativeLayout(基本上;没有必要使用两个RelativeLayout!)。但这似乎有点过度设计,因为似乎可以在不使用其他布局的情况下实现。 - Timmmm

2

你需要将相对布局从

android:layout_height="wrap_content"

为了

android:layout_height="match_parent"

因为你将父元素的高度包裹在子元素中,并使子元素与父元素匹配,所以其高度为0。
如果您希望在相对布局之上/之下有其他内容,则使用Android权重。
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"  <!-- or 0dip -->
        android:layout_weight="1"
        android:background="#000" >

那不是 match_parent 的工作方式。而且我也不想让我的 RelativeLayout 填满其父容器或可扩展。 - Timmmm
@Timmmm 那么,这两个操作都不能使其可见吗? - IAmGroot
实际上,在垂直的LinearLayout中设置layout_height="match_parent"没有任何效果。我认为LinearLayout会忽略它,因为这没有意义。 - Timmmm
由于它是线性布局,最好按照我建议的使用权重。在线性布局中使用match_parent是完全可以接受的,但在这种情况下不适用。 - IAmGroot
我不明白。使用layout_weight="0"会扩展视图,这不是我想要的。而且我应该说match_parentLinearLayout的方向上没有效果,也没有意义,例如在具有orientation="vertical"LinearLayout中使用layout_height="match_parent",这就是我的情况。显然,在另一个方向上可以使用它! - Timmmm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接