首页 > 解决方案 > 如何在滚动android studio上隐藏工具栏

问题描述

我试图让我的应用程序上的工具栏在应用程序上向下滚动时消失。我目前的设置似乎无法弄清楚。我想知道我是否可以通过现在如何设置我的工具栏来完成此操作,或者我是否需要重做我的工具栏?

我试图添加,

app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"

因为我有人告诉我把它放进去,但它似乎不起作用。

我的工具栏:

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

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/global_color_primary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


    </LinearLayout>

标签: javaandroidxmlandroid-studio

解决方案


Toolbar 需要是 AppBarLayout 的子级才能在滚动时隐藏。我相信 AppBarLayout 也必须包含在 CoordinatorLayout 中。

不久前对这个问题的回答有一个很好的工作示例:CoordinatorLayout doesn't hide the actionbar


推荐阅读