工具栏在坐标布局下重叠活动

Mus*_*sir 3 android android-layout android-fragments

在带有一个ViewPager和三个片段的活动中,我想自动隐藏ToolBar. 为此,我正在使用android.support.design.widget.CoordinatorLayout.

但是这ViewPager给了我一些问题。如果在 中CoordinateLayout,则ToolBar与活动重叠。如果它CoordinatorLayoutToolBar活动之外,则不与WebView活动重叠。如果ViewPager是在外CoordinatorLayout,自动隐藏不起作用。

任何帮助,将不胜感激。

活动_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinatorLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <android.support.v7.widget.Toolbar
                android:id="@+id/tool_bar"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:background="@color/ColorPrimary"
                android:elevation="2dp"
                android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways"
                android:fitsSystemWindows="true" />

            <com.taadu.slidechat.adaptor.SlidingTabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/ColorPrimary"/>

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

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我一直在用线性布局尝试,如果ViewPager是在LinearLayout活动并不重叠,但是这消除了自动隐藏功能。

我只添加android.support.v4.widget.NestedScrollView了一个片段来测试自动隐藏。请看一看。

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:isScrollContainer="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ProgressBar
            android:id="@+id/progressBar3"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="3dip"
            android:max="100"
            android:progressDrawable="@drawable/greenprogress" />

        <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webViewTop"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/progressBar3"/>
    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

V-r*_*hit 6

这是因为你在给予layout_behavior你的NestedScrollView而不是你的ViewPager

添加app:layout_behavior="@string/appbar_scrolling_view_behavior"到您的ViewPage

ViewPager应该像

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior>
</android.support.v4.view.ViewPager>
Run Code Online (Sandbox Code Playgroud)