Prevent BottomSheetDialogFragment covering navigation bar

前端 未结 10 509
轮回少年
轮回少年 2020-12-12 23:49

I\'m using really naive code to show a bottom sheet dialog fragment:

class LogoutBottomSheetFragment : BottomSheetDialogFragment() {

    override fun onCrea         


        
10条回答
  •  天命终不由人
    2020-12-13 00:44

    In BottomSheetDialogFragment, the only thing that needs to be done is to set the container of the underlying CoordinatorLayout fitSystemWindows to false.

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        (view!!.parent.parent.parent as View).fitsSystemWindows = false
    }
    
    • view is your layout
    • view.parent is a FrameLayout containing your view
    • view.parent.parent is the CoordinatorLayout
    • view.parent.parent.parent is the container for CoordinatorLayout which has its fitsSystemWindow set to true by default.

    This ensures that the whole BottomSheetDialogFragment is drawn underneath the navigation bar. Then you can set the fitsSystemWindows to your own containers accordingly.

    What you don't need from the other answers in particular is:

    • hacky findViewById with reference to system ids, which are subject to change,
    • reference to getWindow() or getDialog(),
    • no drawables to be set in the place of navigation bar.

    This solution works with BottomSheetDialogFragment created with onCreateView, I did not check onCreateDialog.

提交回复
热议问题