如何更改 bottomsheet 对话框片段的默认高度?

How to change the default height of bottomsheet dialog fragment?

在使用 bottomsheet 对话框片段时,它设置了 bottomsheet 对话框的默认高度。在我的应用程序中,我想为 bottomsheet 对话框设置 80% 的高度。如何将底部对话框的高度设置为 80%?

试试下面的代码。

super.setupDialog(dialog, style);

View contentView = View.inflate(getContext(), R.layout.activity_device_nfclocation, null);

DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();

int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;

int maxHeight = (int) (height*0.88);

BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) contentView.getParent());
mBehavior.setPeekHeight(maxHeight);
dialog.show();

你可以这样做它可以用你需要的任何高度替换 600dp

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
   
    
    <item name="bottomSheetDialogTheme">@style/CustomBottomSheet</item>
    
</style>


<style name="CustomBottomSheet" 
    parent="Theme.MaterialComponents.Light.BottomSheetDialog">
         <item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">

    <item name="behavior_peekHeight">600dp</item>
</style>