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

4

我尝试在ButtonSheetDialogFragment布局中设置Margin,但它不起作用。我已经尝试从布局和编程方式设置Margin,但结果相同。

这是我的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;
            }
        }
     }

ButtonSheetDialogFragment 是什么? - Code-Apprentice
还有,你尝试了什么?发生了什么事? - Code-Apprentice
我无法设置边距。 - Ajay Shrestha
是的,这是我的错误。实际上我想在ButtomSheetDialogFragment布局中设置带有Margin的圆角边框。 - Ajay Shrestha
而且它不起作用,什么也没有发生。 - Ajay Shrestha
显示剩余5条评论
4个回答

12

使用此代码来实现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();

2
您可以通过设置对话框根布局的layoutParams.xxMargin来实现此功能;但是这还需要使用不同的主题:

Java:

@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    
    FrameLayout.LayoutParams layoutParams =
            (FrameLayout.LayoutParams) view.getLayoutParams();
    int margin_10dp = dpToPx(10);
    layoutParams.rightMargin = dpToPx(margin_10dp );
    layoutParams.leftMargin = dpToPx(margin_10dp);

    view.setLayoutParams(layoutParams);
    view.requestLayout();       
    
}

private int dpToPx(int dp) {
    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp,
            r.getDisplayMetrics()
    );
    return px;
}

Kotlin:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    
    val layoutParams: FrameLayout.LayoutParams =
        view.layoutParams as FrameLayout.LayoutParams
    layoutParams.bottomMargin = 32.toPx().toInt()
    layoutParams.rightMargin = 32.toPx().toInt()
    layoutParams.leftMargin = 32.toPx().toInt()
    view.layoutParams = layoutParams
    
}

fun Number.toPx() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this.toFloat(),
    Resources.getSystem().displayMetrics
)   

更改主题:

选项1: 简单明了

使用以下样式以去除背景颜色:

<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowBackground">@null</item>
</style>

然后通过覆盖 getTheme() 将它应用于对话框:

// Kotlin
override fun getTheme(): Int {
    return R.style.NoBackgroundDialogTheme
}

// Java
@Override
public int getTheme() {
    return R.style.NoBackgroundDialogTheme;
}

选项2:

android.R.style.Theme_Translucent主题设置为对话框:

@Override
public int getTheme() {
    // Step 1
    return android.R.style.Theme_Translucent;
}

但这需要再次保留暗淡的背景:
getDialog().getWindow().setBackgroundDrawableResource(R.color.color_trans);

整个类:

Kotlin使用选项1:


class MyDialogFragment() : BottomSheetDialogFragment() {

    override fun getTheme(): Int {
        return R.style.NoBackgroundDialogTheme
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        val view: View = View.inflate(context, R.layout.fragment_bottomsheet, null)
        return view
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        addMargin(view)
    }

    private fun addMargin(view: View) {
        val layoutParams: FrameLayout.LayoutParams =
            view.layoutParams as FrameLayout.LayoutParams
        layoutParams.bottomMargin = 32.toPx().toInt()
        layoutParams.rightMargin = 32.toPx().toInt()
        layoutParams.leftMargin = 32.toPx().toInt()
        view.layoutParams = layoutParams
    }


    fun Number.toPx() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this.toFloat(),
        Resources.getSystem().displayMetrics
    )

}

使用Java选项2:

public class ButtomSheetFragment extends BottomSheetDialogFragment {

    //...

    @Override
    public int getTheme() {
        // Step 1
        return android.R.style.Theme_Translucent;
    }


    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // Step 2
getDialog().getWindow().setBackgroundDrawableResource(R.color.color_trans);
        
        // Step 3
        addMargin(view);
    }

    private void addMargin(View view) {
        FrameLayout.LayoutParams layoutParams =
                (FrameLayout.LayoutParams) view.getLayoutParams();
        int margin_10dp = dpToPx(10);
        layoutParams.rightMargin = dpToPx(margin_10dp );
        layoutParams.leftMargin = dpToPx(margin_10dp);

        view.setLayoutParams(layoutParams);
        view.requestLayout();
    }

    private int dpToPx(int dp) {
        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                r.getDisplayMetrics()
        );
        return px;
    }

}

1

当你设置好边距后,请调用requestLayout()方法。

在您的情况下,可以这样做:

contentView.requestLayout();

在添加左边距后。

0

使用主题:

<style name="BottomSheetWithMargin" parent="ThemeOverlay.Material3.BottomSheetDialog">
        <item name="marginLeftSystemWindowInsets">true</item>
        <item name="marginRightSystemWindowInsets">true</item>
</style>

然后将此样式添加到您的BottomSheetDialogFragment类中。(不幸的是,Bottom margin没有属性)

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