防止回收站视图占用整个 space

Prevent recycler view from taking up whole space

我在片段中使用 RecyclerView 并将其高度设置为 wrap_content 并在其下方设置了一个按钮。但问题是 RecyclerView 占满了整个 space 按钮不可见。出了什么问题?

这是我的 xml 文件 -

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

<TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="17sp"
    android:layout_marginTop="20dp"
    android:visibility="gone"
    android:text="You haven't added any names yet!"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/names_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"/>

<Button
    android:id="@+id/add_name"
    android:layout_width="203dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/recycler_view"
    android:padding="15dp"
    android:textColor="#F3F3F3"
    android:textSize="17sp"/>

</LinearLayout>`

您应该将 weight 设置为 RecyclerView 并将 height 更改为 0dp

<android.support.v7.widget.RecyclerView
android:id="@+id/names_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical"/>

将 RecyclerView 和 Button 包装在 LinearLayout 中,然后添加

android:layout_height="0dp"
android:layout_weight="1"

到 RecyclerView。

您的代码应如下所示:

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

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="You haven't added any names yet!"
        android:textSize="17sp"
        android:visibility="gone"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="false"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/empty_view"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/names_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"/>

        <Button
            android:id="@+id/add_name"
            android:layout_width="203dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/recycler_view"
            android:padding="15dp"
            android:textColor="#F3F3F3"
            android:textSize="17sp"/>

    </LinearLayout>
</LinearLayout>