删除 Android 片段中回收器视图上方不需要的空白区域

bit*_*her 2 android android-recyclerview

我的屏幕上出现了不需要的空白区域,如下所示。

不需要的空间

下面是我的回收器视图中列表项的 xml 代码。

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

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="88dp">


    <ImageView
        android:id="@+id/ivWashListIcon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_dashboard_black_24dp" />

    <TextView
        android:id="@+id/tvWashListTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="Book"
        android:textAlignment="viewStart"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textColor="@color/cardview_dark_background"
        app:layout_constraintStart_toEndOf="@+id/ivWashListIcon"
        app:layout_constraintTop_toTopOf="@+id/ivWashListIcon" />

    <TextView
        android:id="@+id/tvWashListDetail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="56dp"
        android:text="TextView"
        android:textAlignment="viewStart"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/tvWashListTitle"
        app:layout_constraintTop_toBottomOf="@+id/tvWashListTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

下面是片段的代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".ui.wash.WashFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvWashList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这是活动代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

在我的fragment.kt 文件中,我已初始化如下。

   var WashList: RecyclerView = root.findViewById(R.id.rvWashList)
        layoutManager = LinearLayoutManager(activity)
        WashList.layoutManager = layoutManager

        adapter = WashListAdapter()
        WashList.adapter = adapter
Run Code Online (Sandbox Code Playgroud)
我曾尝试将layout_height参数更改为wrap_content和match_parent。但一切都没有改变。请帮忙。

Giu*_*one 5

问题在于您的活动 XML 中的 paddingTop: android:paddingTop="?attr/actionBarSize"> 只需将其删除即可。