【问题标题】:How can I make a `View` fill a `RelativeLayout`?如何让“视图”填充“RelativeLayout”?
【发布时间】:2012-10-08 00:56:36
【问题描述】:

我有一个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 高度了)。

发生了什么事?这是一个错误吗?

【问题讨论】:

    标签: android layout view android-relativelayout


    【解决方案1】:

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

    如果您从视图中删除所有alignParent 属性并仅使用match_parent 作为视图的宽度和高度会发生什么?

    罗尔夫

    最终编辑?:

    小例子,像这样? 使用容器?当内部 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应该填充它。可悲的是,我已经尝试了您的第二个建议。没有快乐。
    • 重新编辑:是的,这就是我的代码目前的样子,也是我拥有的最好的,但这不是我正在寻找的解决方案。如果在RelativeLayout 中添加其他视图使其变大,则背景View 将不再填充它。
    • 编辑了我的帖子,现在我正在使用容器来获得你想要的行为。
    • 是的,我的意思是...当然我可以像你一样使用FrameLayout 来包含背景视图和另一个RelativeLayout(基本上;没有必要使用两个RelativeLayouts !)。但这似乎有点矫枉过正,因为它似乎在没有其他布局的情况下也可以工作。
    【解决方案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 所以做这些都不让它可见?
    • 没有。事实上,将layout_height="match_parent" 设置为垂直的LinearLayout(它所在的位置)没有任何效果。我认为LinearLayout 忽略了这一点,因为它没有任何意义。
    • 因为它是线性布局,最好按照我的建议使用权重。在线性布局中使用 match_parent 是完全可以接受的,只是在这种情况下不行。
    • 我不关注。使用layout_weight="0" 将扩大我不想要的视图。我应该说match_parentLinearLayout 的方向上没有效果,也没有任何意义,例如layout_height="match_parent"LinearLayoutorientation="vertical" 内,这就是我所拥有的。显然,您可以在另一个方向使用它!