如何将MarginBottom添加到BottomSheetDialogFragment?

时间:2016-11-11 05:44:24

标签: android android-layout android-fragments bottom-sheet

这是我目前为BottomSheetFragment所做的。我需要在bottomSheet中添加一个offset offsetBottom。在其视图上设置边距,添加:

  @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;
    }

创建此视图:

enter image description here

如您所见,90dp边距最终为BottomSheet的填充。 如何将偏移量marginBottom应用于BottomSheetDialogFragment

2 个答案:

答案 0 :(得分:0)

将FrameLayout用于第一个ViewGroup。 这样,然后在第二个ViewGroup中设置边距

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_60"
    android:layout_gravity="bottom"
    android:layout_marginBottom="@dimen/dp_66"">

  </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

答案 1 :(得分:-1)

如果可以,请使用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);


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