单Activity多Fragment布局,多次设置fitSystemwindow 无效
1.布局如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainActivity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>
<com.ttyh.worker.view.WindowInsetsFrameLayout
android:id="@+id/bottom_bar_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_bar"
android:layout_below="@id/tv_tittle_top"
android:background="@color/color_FFFFFF" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>
其中WindowInsetsFrameLayout 代码如下,作为Fragment的容器,重发fitSystemwindow的值给子布局
/**
* description:谷歌bug,重写FrameLayout,在测量前将fitSystemWindows值传递给子布局
* version: 1.0
*/
class WindowInsetsFrameLayout @TargetApi(Build.VERSION_CODES.LOLLIPOP) constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int
) :
FrameLayout(context!!, attrs, defStyleAttr) {
@JvmOverloads
constructor(context: Context?, attrs: AttributeSet? = null) : this(context, attrs, 0) {
}
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
val childCount = childCount
for (index in 0 until childCount) {
getChildAt(index).dispatchApplyWindowInsets(insets)
Log.e("debug","onApplyWindowInsets")
}
return insets
}
init {
setOnHierarchyChangeListener(object : OnHierarchyChangeListener {
override fun onChildViewAdded(parent: View, child: View) {
requestApplyInsets()
Log.e("debug","onChildViewAdded")
}
override fun onChildViewRemoved(parent: View, child: View) {}
})
}
}
按照stackOverFlow的相关答案指示,并没有解决我的问题,相关链接:
android - fitsSystemWindows效果消失了,通过FragmentTransaction添加的片段 - Stack Overflow

我的情况和这位老哥一模一样,

本来第3个Fragment顶部图片会进入状态栏的,但是现在还是这样,
按下底部的导航按钮,进行Fragment的切换,第一个和第2个Fragment 的fitsystemWindow = true,第3,4个Fragment的fitsystemWindow = false ,
按照网上的说话,我已经对FrameLayout进行重写了,并重新在测量前将fitSystemWindows值传递给子布局,但是在Fragment切换时并没有生效,只在第一次设置时生效
大概看了一下,还在上班没细看。看样子你要是在沉浸式状态栏,我忘记了,好像是这么个概念。如果你要设置这个fitsystemWindow ,在布局里就可以设置。另外,你可以跑一个demo,然后复制粘贴即可。github上随便找。
如果你是不同的fragment的要求不一样,某些是顶到顶部的,某些是不顶到顶部的,你除了可以动态设置以外,还果以修改主题颜色。但是,一般是统一的吧。要么都是顶到顶部的,要么全部不顶到顶部。