預設情況下,在 Expanded 模式下開啟 BottomSheet DialogFragment

BottomSheet DialogFragment 預設在 STATE_COLLAPSED 中開啟。藉助以下程式碼模板,可以強制開啟 STATE_EXPANDED 並佔用整個裝置螢幕。

@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState){

    BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;

            FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });

    // Do something with your dialog like setContentView() or whatever
    return dialog;
}

儘管對話方塊動畫略顯引人注目,但完成了全屏開啟 DialogFragment 的任務。