如何在 BottomSheetDialogFragment 设置最大高度?

https://img1.sycdn.imooc.com/65a7d0800001869e06221172.jpg我有一个 BottomSheetDialogFragment,我需要在此对话框中设置我的高度。我需要,当用户点击按钮时,将出现对话框并填充屏幕的 85%。这个怎么做?

精慕HU
浏览 131回答 6
6回答

茅侃侃

BottomSheetDialogFragment:override fun onViewCreated(view: View, savedInstanceState: Bundle?) {    super.onViewCreated(view, savedInstanceState)    val offsetFromTop = 200    (dialog as? BottomSheetDialog)?.behavior?.apply {        isFitToContents = false        setExpandedOffset(offsetFromTop)        state = BottomSheetBehavior.STATE_EXPANDED    }}

白衣非少年

你可以这样做:public class MyBottomSheetDialog extends BottomSheetDialogFragment {   //....   @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {    Dialog dialog = super.onCreateDialog(savedInstanceState);    dialog.setOnShowListener(new DialogInterface.OnShowListener() {      @Override public void onShow(DialogInterface dialogInterface) {        BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;        setupRatio(bottomSheetDialog);      }    });    return  dialog;  }  private void setupRatio(BottomSheetDialog bottomSheetDialog) {    //id = com.google.android.material.R.id.design_bottom_sheet for Material Components    //id = android.support.design.R.id.design_bottom_sheet for support librares    FrameLayout bottomSheet = (FrameLayout)         bottomSheetDialog.findViewById(R.id.design_bottom_sheet);    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);    ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();    layoutParams.height = getBottomSheetDialogDefaultHeight();    bottomSheet.setLayoutParams(layoutParams);    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);  }  private int getBottomSheetDialogDefaultHeight() {    return getWindowHeight() * 85 / 100;  }  private int getWindowHeight() {    // Calculate window height for fullscreen use    DisplayMetrics displayMetrics = new DisplayMetrics();    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);    return displayMetrics.heightPixels;  }}

蝴蝶刀刀

始终展开到完整高度可以在BottomSheetDialogFragment()的onCreateDialog中设置&nbsp; &nbsp; override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {&nbsp; &nbsp; val dialog = BottomSheetDialog(requireContext(), theme)&nbsp; &nbsp; dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED&nbsp; &nbsp; dialog.behavior.skipCollapsed = true&nbsp; &nbsp; return dialog}最大高度可以在onCreateView中设置&nbsp; &nbsp; override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,&nbsp; &nbsp; &nbsp; &nbsp; savedInstanceState: Bundle?&nbsp; &nbsp; ): View? {&nbsp; &nbsp; &nbsp; &nbsp; val view = inflater.inflate(R.layout.bottom_sheet_dialog, container, false)&nbsp; &nbsp; &nbsp; &nbsp; // Set max height to half screen&nbsp; &nbsp; &nbsp; &nbsp; view.findViewById<ConstraintLayout>(R.id.root_layout_of_bottom_sheet).maxHeight =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (resources.displayMetrics.heightPixels * 0.5).toInt()&nbsp; &nbsp; &nbsp; &nbsp; return view&nbsp; &nbsp; }

明月笑刀无情

在 Kotlin 和 Jetpack 中编写:val configuration = LocalConfiguration.currentval screenWidth = configuration.screenWidthDp.divideToPercent(0.40f) // total screen width size of 40 percentageval screenHeight = configuration.screenHeightDp.divideToPercent(0.95f) // total screen height size of 95 percentageconfiguration.screenWidth --> 给出屏幕尺寸divideToPercent(0.40f) --> 它将给出屏幕上的一些百分比

白衣染霜花

非常容易处理开始吧override fun onStart() {&nbsp; &nbsp; super.onStart()&nbsp; &nbsp; val height = Resources.getSystem().displayMetrics.heightPixels&nbsp; &nbsp; // in this case I want set max height half of the device height&nbsp; &nbsp; val maxHeight = (height * 0.5).toInt()&nbsp; &nbsp; val behaviour = BottomSheetBehavior.from(requireView().parent as View)&nbsp; &nbsp; /* Note that If you want to display max height&nbsp;&nbsp; &nbsp; &nbsp; as per your bottomsheet view&nbsp;&nbsp; &nbsp; &nbsp; val maxHeight = WindowManager.LayoutParams.MATCH_PARENT */&nbsp; &nbsp; // now set your max peek height&nbsp;&nbsp; &nbsp; behavior.peekHeight = maxHeight&nbsp;}从上面的情况来看,如果你设置maxHeight = WindowManager.LayoutParams.MATCH_PARENT当您在bottmsheet上显示一些固定的内容时,这很简单onCreateView但是有时如果更新UI后Main Thread 它可能不会显示实际高度有时那么最好使用viewTreeObserveroverride fun onStart() {&nbsp; &nbsp; super.onStart()&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; view?.viewTreeObserver?.addOnGlobalLayoutListener {&nbsp; &nbsp; &nbsp; &nbsp; val behavior = BottomSheetBehavior.from(view!!.parent as View)&nbsp; &nbsp; &nbsp; &nbsp; behavior.peekHeight = WindowManager.LayoutParams.MATCH_PARENT&nbsp; &nbsp; }}

斯蒂芬大帝

尝试这个 :&nbsp;DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();&nbsp; &nbsp; &nbsp;int height = displayMetrics.heightPixels;&nbsp; &nbsp; &nbsp;int maxHeight = (int) (height*0.80);&nbsp;View bottomSheet = findViewById(R.id.bottom_sheet);&nbsp;&nbsp;&nbsp;BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;behavior.setPeekHeight(maxHeight);并在您的 XML 中:<LinearLayout&nbsp;&nbsp; &nbsp; android:id="@+id/bottom_sheet"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; app:behavior_hideable="true"&nbsp; &nbsp; app:layout_behavior="android.support.design.widget.BottomSheetBehavior"&nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; .</LinearLayout>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java