Appbar fitsSystemWindows-在ViewPager中 [英] Appbar fitsSystemWindows - Inside a ViewPager

本文介绍了Appbar fitsSystemWindows-在ViewPager中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含ViewPager的活动:

activity.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"/>

</FrameLayout>

此分页器包含的片段(均由相同的布局生成)具有CoordinatorLayoutCollapsingToolbarLayout来提供折叠的图像后标题栏:

The fragments this pager contains – which are all generated from the same layout – have a CoordinatorLayout and CollapsingToolbarLayout to provide a collapsing image-back titlebar:

fragment.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/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <!-- Content here -->

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:elevation="8dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleMarginStart="72dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/photo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:background="@color/photo_placeholder"
                android:fitsSystemWindows="true"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                app:layout_collapseMode="pin"
                android:layout_gravity="bottom">

            </android.support.v7.widget.Toolbar>

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

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

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

在活动布局中没有fitsSystemWindows标志时,片段中的图像会正确显示在系统状态栏的后面,而向上滚动时,文本有时会在状态栏的后面折叠.如果将标记添加到–如上所示–文本问题已解决,但是图像现在在状态栏处被截断.将折叠的工具栏代码保留在片段中,有没有办法使片段的内容适合系统窗口?

Without the fitsSystemWindows flags in the activity layout, the image in the fragment properly displays behind the system status bar, while the text sometimes collapses behind the status bar when scrolled up. If I add the flags in – as shown above – the text issue is resolved, but the image now cuts off at the status bar. Keeping the collapsing toolbar code in the fragment, is there a way to fit the fragment's content to the system window?

活动中没有fitsSystemWindows(首选行为):

Without fitsSystemWindows in the activity (preferred behaviour):

在活动中使用fitsSystemWindows:

推荐答案

您尚未声明,您还希望将NestedScrollView也用作fitSystemWindows.因此,从字面上看,您禁止将WindowInsets传递给NestedScrollView以外的孩子,因此ViewPager甚至都不知道WindowInsets.

You haven't declared, that you want NestedScrollView also to fitSystemWindows. So, literally, you are prohibiting WindowInsets to be passed to children off NestedScrollView, thus ViewPager is not even aware about WindowInsets.

也将android:fitSystemWindows="true"应用于NestedScrollView.

这篇关于Appbar fitsSystemWindows-在ViewPager中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆