android-fragments 如何将MarginBottom添加到BottomSheetDialogFragment中?

u3r8eeie  于 2022-11-14  发布在  Android
关注(0)|答案(4)|浏览(113)

这是我目前所拥有的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;
    }

创建此视图:

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

ohtdti5x

ohtdti5x1#

对第一个ViewGroup使用FrameLayout,就像这样,并在第二个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>
7tofc5zh

7tofc5zh2#

您可以使用CardView作为底部工作表的根视图,然后定义app:cardUseCompatPadding="true",这样可以从底部为它提供一个边距。此外,您还可以定义app:cardElevation="5dp",使其更大,这取决于您希望它有多大

x7rlezfr

x7rlezfr3#

请执行下列操作以使用边距

1:首先,使用XML创建BottomSheetDialogFragment.class
2:将边距设置为样式,并在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>

在final中必须在BottomSheetDialogFragment.类上设置

注意:在BottomSheetDialogFragment.class中设置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);
        
    }

wfypjpf4

wfypjpf44#

如果可以,请对父视图使用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创建一个附加容器,并将视图放置在其中。

相关问题