【问题标题】:CollapsingToolbarLayout inside Viewpager: toolbar acting strangeViewpager 内的 CollapsingToolbarLayout:工具栏表现得很奇怪
【发布时间】:2017-04-20 23:13:25
【问题描述】:

我有一个带有片段的viewpager,每个片段都应该包含一个折叠工具栏+图像标题。

布局很好,但我有工具栏的这种奇怪行为,它有时会忽略顶部边距并在系统栏下结束。发生这种情况:

  • 在第一个选项卡上
  • 滚动到标签 3 或更多标签后,滚动到任何“第 上一个标签”
  • 一次只发生在 1 个标签上(据我所知)

尽管 OnCreateOptionsMenu 正在运行并且没有抛出任何错误,但在第一个选项卡上菜单也没有膨胀。

我多次检查代码并进行了很多调整,但为了我,我找不到我做错了什么!

添加屏幕截图和一些代码。这是a Github repository,如果您想运行它并查看它的实际效果。感谢您的帮助!

当向左滚动时会发生这种情况:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

fragment_main.xml

<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.manzo.tabbednavigation.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/appbar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:title="Title"
            app:contentScrim="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                android:id="@+id/iv_scroll_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:fitsSystemWindows="true"
                android:src="@drawable/bg"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorAccentWarning"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

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

【问题讨论】:

  • 尝试为您的toolbar添加app:layout_scrollFlags="scroll|exitUntilCollapsed"...
  • @rafsanahmad007 刚试过,没有明显变化。

标签: android android-fragments android-viewpager android-collapsingtoolbarlayout


【解决方案1】:

我没有找到解决方案,所以我最终使用了一种解决方法。

我从工具栏中删除了 fitSystemWindows="true" 并添加了 24dp 的 marginTop,这是根据 material guidelines 的系统栏高度,我还为以前的 sdks 添加了 25dp 的高度。

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginTop="@dimen/status_bar_height"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

我愿意接受任何关于“真正”解决方案的建议

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多