在片段交易CoordinatorLayout状态栏填充消失

在片段交易CoordinatorLayout状态栏填充消失

本文介绍了在片段交易CoordinatorLayout状态栏填充消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着22.2.1设计支持库和API 22(在较早版本还没有测试过),我片段之间切换时运行与状态栏填充的问题。最初的片段可罚款,但片段交易后,状态栏填充消失,推高了他们不应该是各方面的意见。在弹出堆栈回来后,同样的事情发生到原来的片段。旋转设备修复它,因为它打开软键盘(仅在纵向,而不是横向)。

初始负载或旋转后,主片段(期望)

回到pressed

主要片段

加载其他片段

旋转或键盘之后,其他片段(期望)

主要片段:

 < android.support.design.widget.CoordinatorLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真
工具:上下文=MainFragment。>

< android.support.design.widget.AppBarLayout
    机器人:ID =@ + ID / appbar
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar>

    < android.support.v7.widget.Toolbar
        机器人:ID =@ + ID /工具栏
        机器人:layout_width =match_parent
        机器人:layout_height =?ATTR / actionBarSize
        应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light/>
< /android.support.design.widget.AppBarLayout>

< android.support.v7.widget.RecyclerView
    机器人:ID =@ + ID / recycler_view
    风格=@风格/ RecyclerView
    应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior/>

< android.support.design.widget.FloatingActionButton
    机器人:ID =@ + ID /晶圆厂
    风格=@风格/晶圆厂
    机器人:SRC =@可绘制/ ic_person_add_white_24dp
    应用程序:backgroundTint =@色/ accent_dark
    应用:边框宽度=2DP/>
 

第二个片段:

 < android.support.design.widget.CoordinatorLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真正的>

< android.support.v4.view.ViewPager
    机器人:ID =@ + ID / viewpager
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:填充=@扪/ card_margin
    应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior/>

< android.support.design.widget.AppBarLayout
    机器人:ID =@ + ID / appbar
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:fitsSystemWindows =真
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar>

    < android.support.design.widget.CollapsingToolbarLayout
        机器人:ID =@ + ID / collapsing_toolbar
        机器人:layout_width =match_parent
        机器人:layout_height =@扪/ collapsingToolbar_height
        机器人:fitsSystemWindows =真
        应用程序:contentScrim =?ATTR / colorPrimary
        应用程序:expandedTitleMarginBottom =@扪/ default_margin
        应用程序:expandedTitleMarginEnd =@扪/ sheet_expanded_title_margin
        应用程序:expandedTitleMarginStart =@扪/ sheet_expanded_title_margin
        应用程序:layout_scrollFlags =滚动| exitUntilCollapsed>

        < android.support.v7.widget.Toolbar
            机器人:ID =@ + ID /工具栏
            机器人:layout_width =match_parent
            机器人:actionBarSizelayout_height =
            应用程序:layout_collapseMode =针
            应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light/>

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

    < android.support.design.widget.TabLayout
        机器人:ID =@ + ID /标签
        机器人:layout_width =match_parent
        机器人:layout_height =@扪/ default_tab_layout_height
        应用程序:tabGravity =中心
        应用程序:tabMinWidth =120dp
        应用程序:tabMode =滚动/>

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

< android.support.design.widget.FloatingActionButton
    机器人:ID =@ + ID /晶圆厂
    风格=@风格/晶圆厂
    机器人:SRC =@可绘制/ ic_add_white_24dp
    应用程序:backgroundTint =@色/ accent_dark
    应用:边框宽度=2DP/>
 

主题有windowDrawsSystemBarBackgrounds为true,statusBarColor透明。

解决方案

解决它,感谢<一href="https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=180492"相对=nofollow>克里斯诅咒。

的问题是,它不知道该窗口边框。你必须在onViewCreated requestApplyInsets。

  @覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    super.onViewCreated(查看,savedInstanceState);
    ViewCompat.requestApplyInsets(coordinatorLayout);
}
 

With the 22.2.1 Design Support Library and API 22(have not tested on earlier versions yet), I'm running into issues with the status bar padding when switching between fragments. The initial fragment loads fine, but after a fragment transaction, the status bar padding disappears, pushing all the views up where they shouldn't be. The same thing happens to the original fragment after popping the back stack. Rotating the device fixes it, as does opening the soft keyboard(but only in portrait, not in landscape).

main fragment on initial load or after rotation(desired)

main fragment after back pressed

other fragment when loaded

other fragment after rotation or keyboard(desired)

main fragment:

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainFragment">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    style="@style/RecyclerView"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Fab"
    android:src="@drawable/ic_person_add_white_24dp"
    app:backgroundTint="@color/accent_dark"
    app:borderWidth="2dp"/>

second fragment:

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/card_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/collapsingToolbar_height"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="@dimen/default_margin"
        app:expandedTitleMarginEnd="@dimen/sheet_expanded_title_margin"
        app:expandedTitleMarginStart="@dimen/sheet_expanded_title_margin"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/default_tab_layout_height"
        app:tabGravity="center"
        app:tabMinWidth="120dp"
        app:tabMode="scrollable"/>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Fab"
    android:src="@drawable/ic_add_white_24dp"
    app:backgroundTint="@color/accent_dark"
    app:borderWidth="2dp" />

Themes have windowDrawsSystemBarBackgrounds to true and statusBarColor to transparent.

解决方案

Solved it, thanks to Chris Banes.

The problem is that it doesn't know the window insets. You have to requestApplyInsets in onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ViewCompat.requestApplyInsets(coordinatorLayout);
}

这篇关于在片段交易CoordinatorLayout状态栏填充消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:55