Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使子视图组与父级高度RecyclerView匹配_Android_Android Recyclerview - Fatal编程技术网

Android 使子视图组与父级高度RecyclerView匹配

Android 使子视图组与父级高度RecyclerView匹配,android,android-recyclerview,Android,Android Recyclerview,我正在尝试创建一个RecyclerView项目布局,其中子视图组应与其所在项目的高度相匹配,例如,我有一个250dp高度的项目,我有一个线性布局,其高度设置为match\u parent其高度也应为250dp 当我尝试这样做时,情况似乎并非如此,LinearLayout高度似乎被强制wrap\u content 这是我用来测试的示例布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="h

我正在尝试创建一个RecyclerView项目布局,其中子视图组应与其所在项目的高度相匹配,例如,我有一个250dp高度的项目,我有一个线性布局,其高度设置为
match\u parent
其高度也应为250dp

当我尝试这样做时,情况似乎并非如此,LinearLayout高度似乎被强制
wrap\u content

这是我用来测试的示例布局

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_toLeftOf="@+id/ll"
            android:background="@color/android_green">

        </RelativeLayout>

        <LinearLayout
            android:layout_width="175dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:id="@+id/ll"
            android:layout_alignParentRight="true"
            android:background="@color/android_red">

        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

我只在设置硬高度(250dp)时看到线性布局,如果我将其更改为与父项匹配,它将消失


这在RecyclerView中不可能了吗?

你说你希望它是250dp,对吗?问题是,你的线性布局在二级(二级)相对论中。设置一个匹配项将使其从那里复制高度,而不是硬编码为250dp的三阶相对长度

我建议保持这样,并在java中扩展它,这样你的应用程序就可以在所有设备上运行。。。硬编码数字并不是最好的方法。。。但是,如果您需要精确地将其设置为250dp,请将线性布局设置为3阶RealativeLayout的子级

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

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_toLeftOf="@+id/ll"
            android:background="@color/android_green">

            <LinearLayout
                android:layout_width="175dp"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:id="@+id/ll"
                android:layout_alignParentRight="true"
                android:background="@color/android_red">
            </LinearLayout>

        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

更新


我完全同意Mike M.关于删除第二级RelativeLayout的评论。它没有任何明显的用途(至少在提供的代码中没有),并且无用地使您的程序复杂化。如果你真的需要它,试着找一些不那么凌乱的替代布局。

你能解释一下你面临的问题吗?如果您有下面给出的问题,请按如下所示进行更改

请将您的父布局(LinearLayout的父布局)即相对布局高度更改为250dp,现在是match_parent,因此您将获得与250dp相同的两个布局大小。 下面把修改过的代码放在这里,你的意图就是喜欢它,然后把它改成

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="250dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/ll"
        android:background="@color/color_dark_yellow">

    </RelativeLayout>

    <LinearLayout
        android:layout_width="175dp"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/ll"
        android:layout_alignParentRight="true"
        android:background="@color/color_green_txt">

    </LinearLayout>
</RelativeLayout>


使用下面的方法使子视图组与父视图高度匹配:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/rl_child"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_toLeftOf="@+id/ll"
            android:background="#00FF00">

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ll"
            android:layout_width="175dp"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/rl_child"
            android:layout_alignParentRight="true"
            android:background="#FF0000"
            android:orientation="horizontal">

        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>


你能再详细说明一下你面临的问题吗?@subrahmanyamboyapati线性布局与单元格的高度不匹配它总是与内容的高度相匹配
RelativeLayout
有时令人痛苦,尤其是在嵌套时。尝试将
LinearLayout
layout\u alignParentTop
layout\u alignParentBottom
属性都设置为
true
@MikeM。是的,我试过了,结果是视图没有保持与顶部对齐,只是与底部对齐。奇怪的是,我刚刚注意到您有一个
相对视图,它实际上没有做任何事情。你有没有试过把它去掉?嵌套的
RelativeLayout
s的问题通常来自它们在进行布局过程时对自身所做的调整,因此通常越少越好。