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

时间:2016-09-23 23:16:03

标签: android layout fragment

我尝试在ButtonSheetDialogFragment布局中设置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;
            }
        }
     }

3 个答案:

答案 0 :(得分:2)

将此代码用于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();

答案 1 :(得分:0)

设置好保证金后,请致电requestLayout()

在你的情况下,像

contentView.requestLayout();
添加左边距后

答案 2 :(得分:0)

您可以通过设置对话框根布局的 layoutParams.xxMrgin 来实现;但他也需要使用不同的主题:

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

科特林:

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

}