如何在BottomSheetDialogFragment中添加MarginBottom?

7

这是我为我的BottomSheetFragment编写的代码。我需要添加一个向下偏移的marginBottom到bottomSheet上。在视图上设置margin,会添加:

  @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的填充。如何对BottomSheetDialogFragment应用偏移量marginBottom


你找到解决方案了吗?我们可以为BottomSheetDialog设置marginBottom吗? - TOP
4个回答

1

如何使用边距
1:首先,用XML创建BottomSheetDialogFragment.class
2:将边距放置在Style中,并在BottomSheetDialogFragment.class中进行设置


样式

   <!-- Stuff to make the bottom sheet with round top borders  | margins top right left bottom-->
    <style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
    </style>

    <style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:layout_marginStart">25dp</item>
        <item name="android:layout_marginEnd">25dp</item>
        <item name="android:layout_marginBottom">0dp</item>

    </style>

最终必须设置在BottomSheetDialogFragment类中
注意:在BottomSheetDialogFragment类的onCreate方法中进行设置。

  @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //bottom sheet round corners can be obtained but the while background appears to remove that we need to add this.
        setStyle(STYLE_NO_FRAME, R.style.BottomSheetDialog);
        
    }

enter image description here


0
你可以使用 CardView 作为底部菜单的根视图,然后定义 app:cardUseCompatPadding="true"。这样就可以从底部给它一个边距。 此外,你还可以定义 app:cardElevation="5dp",以使其更大,具体取决于你想要多大。

0
使用FrameLayout作为你的第一个ViewGroup,就像这样,并在你的第二个ViewGroup中设置margin。
<?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>

-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);


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


这个问题不涉及RelativeLayout或FrameLayout,只是一个关于margin的问题,我们必须使用样式并将其设置在BottomSheetDialogFragment上进行解决。也许你不清楚我的问题,所以我在答案中写出了解决方案。 - Amin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接