如何将 MarginBottom 添加到 BottomSheetDialogFragment?

Irs*_*shu 5 android android-layout android-fragments bottom-sheet

到目前为止,这是我的BottomSheetFragment。我需要在bottomSheet 中添加一个偏移marginBottom。在其视图上设置边距,添加:

  @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.layou_menu_more, null);
        dialog.setContentView(contentView);
        BottomSheetBehavior<View> mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
        if (mBottomSheetBehavior != null) {
            mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
            mBottomSheetBehavior.setHideable(true);
            contentView.requestLayout();
        }

        View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
        FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,0,0,90);
        bottomSheet.setLayoutParams(params);
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogINterface) {
                dialog.getWindow().setLayout(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
               // dialog.getWindow().setGravity(Gravity.RIGHT);
            }
        });
        return dialog;
    }
Run Code Online (Sandbox Code Playgroud)

创建此视图:

在此处输入图片说明

如您所见,90dp 的边距最终作为BottomSheet 的填充。如何申请偏移marginBottomBottomSheetDialogFragment

Suh*_*Lee -2

如果可以的话,使用RelativeLayout而不是FrameLayout用于父视图。
通过这样做,您可以轻松实现您的目标。

RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomSheet.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)


如果您必须使用,请使用和FrameLayout创建一个附加容器并将您的视图放在那里。height = MATCH_PARENTgravity = BOTTOM