android - 如何在 ButtomSheetDialogFragment Android 中设置左右边距?

标签 android layout fragment bottomsheetdialogfragment

我尝试在 ButtonSheetDialogFragment 布局中设置边距,但它不起作用。我试图通过布局和编程方式设置边距,但结果相同

这是我的 XML 文件 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

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

关于android - 如何在 ButtomSheetDialogFragment Android 中设置左右边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39670847/

相关文章:

Android 4.4 — 半透明状态/导航栏 — fitSystemWindows/clipToPadding 不能通过 fragment 事务工作

android - ListFragment onCreate 调用了两次

android - 在 ViewPager 中使用 EventBus 时获取混合数据

android - 添加平台 android 时出错 : ShellCommandError: Non-zero exit from subprocess

Android工作室错误java.lang.IllegalArgumentException : Resource name cannot be empty

html - 带侧边栏的布局

html - 使用 CSS 将图像定位在以下 <div> 之上

java - android 数据绑定(bind) - 找不到类 android.view.data

java - 如何创建无卡全屏 Android Wear 应用

java - LibGDX - tile/tilemaps 中的纹理文本