首页 > 解决方案 > 将 ConstraintLayout 放置在 NestedScrollView 中时如何使用整个空间?

问题描述

我试图把 a 放在 aConstraintLayout里面NestedScrollView,但是有一条线将ConstraintLayout. 我不能在那条看不见的线下面放任何东西。我已经尝试了几个小时,但找不到问题。

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/premium_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#BFFFFFFF"
        android:focusableInTouchMode="true"
        android:tag="layout/fragment_premium"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".PremiumFragment">

            <ImageView
                android:id="@+id/launcher_id"
                android:layout_width="185dp"
                android:layout_height="185dp"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="20dp"
                android:contentDescription="TODO"
                app:layout_constraintBottom_toTopOf="@+id/premium_text_id"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/launch_premium" />

            <TextView
                android:id="@+id/premium_text_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="220dp"
                android:text="@string/launch_to_premium"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView2"
                android:layout_width="383dp"
                android:layout_height="196dp"
                android:layout_marginTop="24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/premium_text_id" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</layout>

标签: androidandroid-nestedscrollview

解决方案


您需要android:fillViewport="true"在您的NestedScrollView

<androidx.core.widget.NestedScrollView
        android:id="@+id/premium_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#BFFFFFFF"
        android:focusableInTouchMode="true"
        android:fillViewport="true" <-- add this
        android:tag="layout/fragment_premium">

为什么?
fillViewport在滚动视图的子视图高度较小的情况下,允许 NestedScrollView 将其高度扩展为设备屏幕高度的完整高度。
这个博客将对此进行更多解释。


推荐阅读