滚动隐藏工具栏

时间:2017-03-26 16:09:59

标签: android toolbar

我知道很多时候都会问过这个问题,但我仍然无法让它发挥作用。 app:layout_scrollFlags ="滚动| 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 个答案:

答案 0 :(得分:0)

  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

  4. 更新您的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>
    

    希望这会有所帮助!