簡體   English   中英

將 BottomSheetDialogFragment 高度設置為全屏

[英]Set BottomSheetDialogFragment height to full screen

如何讓bottomSheet占據屏幕的整個高度? 設置窺視高度無效。

任何幫助,將不勝感激。

bottomSheetDialogFragment.getDialog().setOnShowListener((dialog) ->
{
    final BottomSheetDialog bottomSheetDialog = (BottomSheetDialog)dialog;
    final FrameLayout bottomSheet = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
    if (bottomSheet != null)
    {
        final BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        behavior.setPeekHeight(30000); // no effect, bottom sheet does not span entire height of screen
    }
});

底部工作表布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <!-- rest of layout not shown -->
    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bottomSheetHandle"
        tools:layout_height="48dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

您可以獲得指標以訪問以像素為單位的屏幕高度,並使用該參考來設置底部表的高度。

獲取指標

val metrics = DisplayMetrics()
requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)

設置對話框的 state 和 peekHeight

bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.behavior.peekHeight = metrics.heightPixels

設置視圖的高度,請注意我們如何將此高度設置為與對話框的 peekHeight 相同。 當你想要一個單一尺寸的 BottomSheetDialog 時,我發現這是最好的方法

bottomSheet.layoutParams.height = metrics.heightPixels
bottomSheet.requestLayout()
// add this code into your class

    @Override
        public void onStart() {
            super.onStart();
            Dialog dialog = getDialog();
            View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
            if (dialog != null) {
        
                bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            View view = getView();
            view.post(() -> {
                View parent = (View) view.getParent();
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                CoordinatorLayout.Behavior behavior = params.getBehavior();
                BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
                bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
                ((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT)
    
            });
        }

首先在onCreateDialog里面

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    ...
    bottomSheetBehavior?.skipCollapsed = true
    bottomSheetBehavior?.peekHeight = Resources.getSystem().displayMetrics.heightPixels
    bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
    return bottomSheet
}

之后,在啟動方法上使用它

/**
 * to make sheet height full screen
 * */
override fun onStart() {
    super.onStart()
    val metrics = DisplayMetrics()
    requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)
    binding.rootContainer.layoutParams.height = metrics.heightPixels
    binding.rootContainer.requestLayout()
}

我希望它能正常工作,因為它對我來說可以正常工作;)

為此,您可以將 match_parent 設置為底部工作表的布局參數,如下所示:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
                    val dialog = BottomSheetDialog(requireContext(), theme)
                    dialog.setOnShowListener {
                
                        val bottomSheetDialog = it as BottomSheetDialog
                        val parentLayout =
                            bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
                        parentLayout?.let { it ->
                            val behaviour = BottomSheetBehavior.from(it)
                            setupFullHeight(it)
                            behaviour.state = BottomSheetBehavior.STATE_EXPANDED
                        }
                    }
                    return dialog
                }
                
                private fun setupFullHeight(bottomSheet: View) {
                    val layoutParams = bottomSheet.layoutParams
                    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
                    bottomSheet.layoutParams = layoutParams
                }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM