BottomSheetDialogFragment doesn't show full height in landscape mode

前端 未结 5 725
感情败类
感情败类 2020-12-28 18:13

I am using BottomSheetDialogFragment in my activity, the dialog shows full height in portrait mode but doesn\'t when I switch to landscape mode.

相关标签:
5条回答
  • 2020-12-28 18:36

    This is a problem of gestureInsetBottomIgnored. For landscape this flag is false, but for portrait is true.

    0 讨论(0)
  • 2020-12-28 18:41

    This worked for me and was the cleanest approach:

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = BottomSheetDialog(requireContext(), theme)
        dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
        return dialog
    }
    
    0 讨论(0)
  • 2020-12-28 18:47

    the solution for this issue is.

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < 16) {
                    view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
                FrameLayout bottomSheet = (FrameLayout)
                dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                behavior.setPeekHeight(0); // Remove this line to hide a dark background if you manually hide the dialog.
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-28 18:48

    Thanks to @avez raj and Prevent dismissal of BottomSheetDialogFragment on touch outside I wrote in onCreateDialog().

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
    
        dialog.setOnShowListener {
            // For AndroidX use: com.google.android.material.R.id.design_bottom_sheet
            val bottomSheet = dialog.findViewById<View>(
                android.support.design.R.id.design_bottom_sheet) as? FrameLayout
            val behavior = BottomSheetBehavior.from(bottomSheet)
            behavior.state = BottomSheetBehavior.STATE_EXPANDED
        }
    
        return dialog
    }
    
    0 讨论(0)
  • 2020-12-28 18:58

    The ViewTreeObserver solution did not work for me, but I found a superior solution here and converted it to Kotlin. This one doesn't have the expensive computational waste which comes with a ViewTreeObserver and nicely bundles the functionality into the class.

    class ExpandedBottomSheetDialog(context: Context) : BottomSheetDialog(context) {
    
        override fun show() {
            super.show()
            // androidx should use: com.google.android.material.R.id.design_bottom_sheet
            val view = findViewById<View>(R.id.design_bottom_sheet)
            view!!.post {
                val behavior = BottomSheetBehavior.from(view)
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题