不占用整个宽度的 Android BottomSheetDialogFragment

Ufo*_*oXp 6 android android-layout android-fragments

如何制作不覆盖整个屏幕宽度的 BottomSheetDialogFragment(类似于平板电脑上 Chrome 中的“共享通过”表)?

Rob*_*rdi 5

正如@UfoXp 链接的问题中的开发人员所建议的那样,问题是BottomSheetDialog.onCreate()将窗口设置为MATCH_PARENT双向:

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);

    if ((isTablet(getContext()) || isLandscape(getContext()))) {
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogINterface) {
                dialog.getWindow().setLayout(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
            }
        });
    }
    return dialog;
}

private boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

private boolean isLandscape(Context context) {
    return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
Run Code Online (Sandbox Code Playgroud)