fitsSystemWindows effect gone for fragments added via FragmentTransaction

前端 未结 6 1048

I have an Activity with navigation drawer and full-bleed Fragment (with image in the top that must appear behind translucent system bar on Lollipop). While I had an interim

6条回答
  •  猫巷女王i
    2020-11-28 03:06

    Another approach written in Kotlin,

    The problem:

    The FrameLayout you are using does not propagate fitsSystemWindows="true" to his childs:

    
    

    A solution:

    Extend FrameLayout class and override the function onApplyWindowInsets() to propagate the window insets to attached fragments:

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    class BetterFrameLayout : FrameLayout {
    
        constructor(context: Context) : super(context)
    
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    
        constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
    
        override fun onApplyWindowInsets(windowInsets: WindowInsets): WindowInsets {
            childCount.let {
                // propagates window insets to children's
                for (index in 0 until it) {
                    getChildAt(index).dispatchApplyWindowInsets(windowInsets)
                }
            }
            return windowInsets
        }
    }
    

    Use this layout as a fragment container instead of the standard FrameLayout:

    
    

    Extra:

    If you want to know more about this checkout Chris Banes blog post Becoming a master window fitter.

提交回复
热议问题