android - 坐标布局下工具栏重叠activity

标签 android android-layout android-fragments

在一个包含 ViewPager 和三个 fragment 的 Activity 中,我想自动隐藏 ToolBar。为此,我正在使用 android.support.design.widget.CoordinatorLayout

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

如有任何帮助,我们将不胜感激。

activity_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>

我一直在尝试线性布局,如果 ViewPagerLinearLayout 中,activity 不会重叠,但这会删除自动隐藏的功能。

我只在一个 fragment 中添加了 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>

最佳答案

这是因为您将 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>

关于android - 坐标布局下工具栏重叠activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430171/

相关文章:

Android Material Date Range Picker - 如何仅更改选定范围日期的文本颜色?

android - 如何从Android中的日历中提取日期?

android - 如何在 Android Studio Navigation Drawer 中设置默认 fragment ?

android - 错误 : File google-services. 缺少 json。没有它,Google 服务插件将无法运行。

java - 在android中使用浮点值设置布局边距

android - Observable 似乎在一段时间后不会调用 onNext

android - 具有渐变填充和曲线形状的 Canvas 绘图

java - Android - 如何从 Fragment 访问父 Activity 中的 getter 方法

java - 如何播放、停止和暂停在 android 中的媒体播放器实例上工作?

android - 如何在没有 GPS 的情况下跟踪用户从一点到另一点的移动? (移动设备)