如何在ButtomSheetDialogFragment Android中设置左右边距? [英] how to set left and right margin in ButtomSheetDialogFragment Android?

查看:475
本文介绍了如何在ButtomSheetDialogFragment Android中设置左右边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ButtonSheetDialogFragment布局中设置Margin,但是它不起作用.我试图从布局和编程设置边距,但结果相同 这是我的XML文件layout_bts_item.xml

I tried to set Margin in ButtonSheetDialogFragment layout but its not working. I have tried to set margin from layout and programatically but its has same result This is my XML file layout_bts_item.xml

<?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="match_parent"
    android:layout_margin="10dp"
    android:background="#00000000">
   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>
</FrameLayout>

这是我的Java代码

        public class ButtomSheetFragment extends BottomSheetDialogFragment {

        private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
          @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState)      {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dismiss();
                }

            }

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

        @Override
        public void setupDialog(Dialog dialog, int style) {
            super.setupDialog(dialog, style);

            View contentView = View.inflate(getContext(),         R.layout.layout_bts_item, null);
            dialog.setContentView(contentView);
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
            CoordinatorLayout.Behavior behavior = params.getBehavior();
            if (behavior != null && behavior instanceof BottomSheetBehavior) {
                ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
                int height = LayoutUtils.getScreenHeight(getActivity());
                double desiredHeight = (0.85 * height);
                ((BottomSheetBehavior) behavior).setPeekHeight((int) desiredHeight);
                contentView.getLayoutParams().height = (int) desiredHeight;
                ((FrameLayout.LayoutParams)   contentView.getLayoutParams()).leftMargin = 100;
            }
        }
     }

推荐答案

将此代码用于BottomSheetDialog

Use this code for BottomSheetDialog

<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="behavior_peekHeight">350dp</item>
    <item name="android:layout_margin">30dp</item>
</style>
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.show();

这篇关于如何在ButtomSheetDialogFragment Android中设置左右边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆