How to set maximum expanded height in android support design bottom sheet?

前端 未结 4 1688
臣服心动
臣服心动 2020-12-24 06:46

I already showed my bottom sheet layout with its peek height set to 100dp. But how can I limit my bottom sheet to expand to 500dp only? This is my sample layout:

<         


        
4条回答
  •  情歌与酒
    2020-12-24 07:05

    If using BottomSheetDialogFragment:

    In my case I was using BottomSheetDialogFragment and modified the bottom sheet peek height by getting its reference by override OnCreateDialog method inside my BottomSheetFragment

    @NonNull @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
    
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog bottom_dialog = (BottomSheetDialog) dialog;
    
                FrameLayout bottomSheet = (FrameLayout) bottom_dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
                assert bottomSheet != null;
                //BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                DisplayMetrics displayMetrics = requireActivity().getResources().getDisplayMetrics();
                int height = displayMetrics.heightPixels;
                int maxHeight = (int) (height*0.80);
                BottomSheetBehavior.from(bottomSheet).setPeekHeight(maxHeight);
            }
        });
    
        return dialog;
    }
    

提交回复
热议问题