当其他视图小于一个屏幕时,BottomNavigationView 向上移动

BottomNavigationView moves up when other views are smaller than one screen

我在 ConstraintLayout 中有一个 imageView、ProgressBar、TextView 和 RecyclerView,我想让它可以滚动,但我不希望我的 BottomNavigationView 移动。当内容小于一个屏幕时,我不确定如何让它保持向下。我尝试了此处提出的类似解决方案,例如 NestedScrollView,但它们只会让一些视图消失。

我不知道我的代码贴得好不好,所以这里是Github link:https://github.com/PiotrDawidziuk/QuizO2Api/blob/master/app/src/main/res/layout/activity_take_quiz.xml

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="match_parent"
tools:context=".activities.DetailActivity">
<android.support.constraint.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"
tools:context=".activities.TakeQuizActivity">

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/simpleProgressBar"
    />

<ImageView
    android:id="@+id/take_quiz_question_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/message"
    app:layout_constraintStart_toStartOf="parent"
    />

<android.support.v7.widget.RecyclerView
    android:id="@+id/take_quiz_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/take_quiz_question_image"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintEnd_toEndOf="parent"
    >
</android.support.v7.widget.RecyclerView>


<android.support.design.widget.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"/>
 </android.support.constraint.ConstraintLayout>
</ScrollView>

我稍微更改了代码,将 ScrollView 放在 ConstraintLayout 中,并在其他所有内容周围放置了 LinearLayout。它滚动了一点,但出于某种原因它切断了 RecyclerView 的底部:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
tools:context=".activities.DetailActivity">
<ScrollView
    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="0dp"
    tools:context=".activities.TakeQuizActivity"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <ProgressBar
        android:id="@+id/simpleProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/simpleProgressBar"
        />

    <ImageView
        android:id="@+id/take_quiz_question_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/message"
        app:layout_constraintStart_toStartOf="parent"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/take_quiz_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/take_quiz_question_image"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintEnd_toEndOf="parent"
        >
    </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</ScrollView>
<android.support.design.widget.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"/>
</android.support.constraint.ConstraintLayout>

您应该将 scrollView 移到 constaraintLayout 内。但是,在您的代码中,情况恰恰相反。看看

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ScrollView
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
    >

        <android.support.constraint.ConstraintLayout android:layout_width="match_parent"
                                                           android:layout_height="wrap_content">
            <ProgressBar
                    android:id="@+id/simpleProgressBar"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
            />

            <TextView
                    android:id="@+id/message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/simpleProgressBar"
            />

            <ImageView
                    android:id="@+id/take_quiz_question_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@+id/message"
                    app:layout_constraintStart_toStartOf="parent"
            />

            <android.support.v7.widget.RecyclerView
                    android:id="@+id/take_quiz_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@+id/take_quiz_question_image"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintBottom_toTopOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
            >
            </android.support.v7.widget.RecyclerView>
        </android.support.constraint.ConstraintLayout>

    </ScrollView>
    <android.support.design.widget.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"/>
</android.support.constraint.ConstraintLayout>