布局预览与实际设备不符

Layout preview and actual device do not match

我不知道为什么会这样。我敢打赌这真的很简单,但我不知道为什么。我有一个评论 activity,其中一个相对布局作为父级,一个 RecyclerView 以及一个线性布局(有 2 个子级:一个编辑文本和一个提交按钮)作为子级。我的layout.xml如下

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.erik.appname.CommentActivity">


<android.support.v7.widget.RecyclerView
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="470dp"
    android:id="@+id/posted_comments">

</android.support.v7.widget.RecyclerView>



<LinearLayout
    android:id="@+id/write_comment"
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/posted_comments"
    android:layout_alignParentStart="true">

    <EditText
        android:layout_alignParentBottom="true"
        android:paddingLeft="10dp"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:background="@drawable/input_outline2"
        android:layout_height="match_parent"
        android:inputType="textPersonName|textMultiLine"
        android:text=""
        android:gravity="left|center"
        android:hint="Comment..."
        android:textSize="20dp"
        android:maxLength="100"
        android:id="@+id/current_comment" />

    <ImageButton
        android:src="@drawable/submit"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:background="@drawable/input_outline2"
        android:layout_alignParentBottom="true"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:id="@+id/submit"
        android:layout_toEndOf="@+id/current_comment"
        />


</LinearLayout>

这是我从预览中得到的(我希望它看起来像) preview

虽然这是实际设备的屏幕截图real device

为什么 recyclerview 下方的编辑文本和按钮会这样?

我尝试了很多方法,例如将所有内容都包装在线性布局中,将 RelativeLayout 包装在线性布局中,将包含编辑文本和按钮的线性布局包装在另一个布局中,并尝试将它们堆叠起来并使用 layout_weight 确定比例...好像想不出正确的做法

非常感谢您的帮助!

您已将 RecyclerView 高度指定为 android:layout_height="470dp",该高度限制为 470dp,在达到预定义的高度后,其余高度将留在底部 LinearLayout .定义硬代码高度不是一个好主意。尝试将 RecyclerViewheightwidth 删除到 match_parent 它会起作用。

已解决的代码[使用您的下一个值进行编辑]:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_comment"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/posted_comments"
    android:layout_above="@+id/write_comment">

</android.support.v7.widget.RecyclerView>


<LinearLayout
    android:id="@+id/write_comment"
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true">

    <EditText
        android:paddingEnd="10dp"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:background="@mipmap/ic_launcher"
        android:layout_height="match_parent"
        android:inputType="textPersonName|textMultiLine"
        android:text=""
        android:gravity="left|center"
        android:hint="Comment..."
        android:textSize="20sp"
        android:maxLength="100"
        android:id="@+id/current_comment" />

    <ImageButton
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:background="@mipmap/ic_launcher"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:id="@+id/submit"
        android:layout_toEndOf="@+id/current_comment"
        />


</LinearLayout>
</RelativeLayout>