使用支持库中的 BottomSheet 时如何使背景变暗?

How to dim background when using the BottomSheet from the support library?

如何将背景调暗,就像显示的那样here

我已经使用 CoordinatorLayoutBottomSheetBehavior 正常设置了它。

使用BottomSheetDialog

基本上它是一个对话框(即支持暗淡)并支持模态底部表单行为试试看

这只会显示底部 sheet。

public class MyBottomSheet extends BottomSheetDialogFragment {
    private static final String TAG = "MyBottomSheet";

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

        final View view = View.inflate(getContext(), R.layout.my_custom_view, null);

        dialog.setContentView(view);
        behavior = BottomSheetBehavior.from((View) view.getParent());

        return dialog;
    }

    public void show(final FragmentActivity fragmentActivity) {
        show(fragmentActivity.getSupportFragmentManager(), TAG);
    }
}

像正常调用一样关闭对话框 close()

    bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {}

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                            (your_layout/view).setAlpha(1 - slideOffset);

                }
            });