Android fitsSystemWindows not working when replacing fragments

前端 未结 4 412
说谎
说谎 2021-01-01 13:30

I have SingleFramgnetActivity whose purpose is only to hold and replace fragments inside it.

layout looks like this:



        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 13:58

    a) you can use CoordinatorLayout as your root view inside fragment

    or

    b) you can create custom linear layout what will call requestApplyInsets and use it as your root view inside fragment

    class WindowInsetsLinearLayout : LinearLayout {
        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        override fun onAttachedToWindow() {
            super.onAttachedToWindow()
            ViewCompat.requestApplyInsets(this)
        }
    }
    

    and then inside fragment you can catch applying insets

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ViewCompat.setOnApplyWindowInsetsListener(root_layout) { _, insets ->
            //appbar.setPadding(insets.systemWindowInsetLeft, insets.systemWindowInsetTop, 0, 0)
            insets.consumeSystemWindowInsets()
        }
    }
    

提交回复
热议问题