在滚动条上隐藏工具栏

Hide Toolbar on scroll

我知道这个问题已经被问过很多次了,但我仍然无法让它工作。 app:layout_scrollFlags="scroll|enterAlways"参数是有的,不过我觉得跟嵌套有关系。我的 XML 文件如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
  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:background="@color/colorBG"
  android:fitsSystemWindows="true"
  android:id="@+id/mDrawerLayout"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  tools:context=".MainActivity">

  <android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

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

        <!-- App Bar-->
        <android.support.v7.widget.Toolbar
            android:background="@color/colorPrimary"
            android:id="@+id/mToolbar"
            android:layout_height="50dp"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/AppTheme.Toolbar"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:background="@color/colorPrimaryLight"
    android:id="@+id/mNavigationView"
    android:layout_gravity="start"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    app:headerLayout="@layout/header"
    app:itemBackground="@color/colorPrimaryLight"
    app:itemIconTint="@color/colorText"
    app:itemTextColor="@color/colorText"
    app:menu="@menu/drawer"/>

</android.support.v4.widget.DrawerLayout>
  1. 删除 LinearLayout 并添加 android.support.design.widget.AppBarLayout 作为 ToolBar 的容器。

  2. FrameLayout 保持在 android.support.design.widget.AppBarLayout 之外。

  3. app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到FrameLayout

更新您的 activity_main.xml 如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:background="@color/colorBG"
    android:fitsSystemWindows="true"
    android:id="@+id/mDrawerLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

       <!-- App Bar-->
       <android.support.design.widget.AppBarLayout
           android:id="@+id/appbar"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:fitsSystemWindows="true">

           <!-- ToolBar-->
           <android.support.v7.widget.Toolbar
               android:background="@color/colorPrimary"
               android:id="@+id/mToolbar"
               android:layout_height="50dp"
               android:layout_width="match_parent"
               android:minHeight="?attr/actionBarSize"
               android:theme="@style/AppTheme.Toolbar"
               app:layout_scrollFlags="scroll|enterAlways"/>
      </android.support.design.widget.AppBarLayout>

      <!-- Content-->
      <FrameLayout
          android:id="@+id/containerView"
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">
      </FrameLayout>
  </android.support.design.widget.CoordinatorLayout>

  <android.support.design.widget.NavigationView
      android:background="@color/colorPrimaryLight"
      android:id="@+id/mNavigationView"
      android:layout_gravity="start"
      android:layout_height="match_parent"
      android:layout_width="wrap_content"
      app:headerLayout="@layout/header"
      app:itemBackground="@color/colorPrimaryLight"
      app:itemIconTint="@color/colorText"
      app:itemTextColor="@color/colorText"
      app:menu="@menu/drawer"/>

</android.support.v4.widget.DrawerLayout>

希望这会有所帮助!