首页 > 解决方案 > 为什么我的内容显示在我的 Appbar 下方?

问题描述

我正在尝试添加一个 AppBarLayout,当不需要滚动(或内容尚未滚动)时,elavation 为 0,但是当用户可以滚动或滚动时,则会显示 elavation(如 Google Tasks应用程序)。在新材料 Appbar 文档中,我发现将app:liftOnScroll="true"属性添加到我的 Appbar 就可以了,但是在添加之后,我的 NestedScrollView 的内容显示在工具栏上方:

在此处输入图像描述

我面临的另一个问题是,即使我不使用app:liftOnScroll="true",我的 NestedScrollView 也没有显示在 Appbar 的底部: 在此处输入图像描述

这是我的片段的 xml:

<?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="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    tools:context=".ResetPasswordFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar_resetPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true"
        >

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView_resetPassword"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <!-- Some Content -->

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

我知道如果我添加layout_marginTop="?attr/actionBarSize"到我的 NestedScrollView 问题将得到解决,但我已经看到了很多示例,他们创建了我想要的视图而不添加它。

这是我在活动中使用的风格

<style name="AppTheme.NoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="android:statusBarColor">#fff</item>
        <item name="android:windowLightStatusBar">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:fontFamily">@font/barlow</item>
    </style>

提前致谢!

标签: androidandroid-coordinatorlayoutappbar

解决方案


appbar_scrolling_view_behaviorCordinatorLayout. 所以你需要使用CordinatorLayout作为父母而不是如果ConstraintLayout..


推荐阅读