简体   繁体   中英

Android bottomsheet dialog how to set bottom margin

I can set right and left margin of bottomsheet dialog. Is there any ways to put margin bottom of the bottom sheet dialog?

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog d = super.onCreateDialog(savedInstanceState);
        d.setOnShowListener(new DialogInterface.OnShowListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onShow(DialogInterface dialogInterface) {
                View content = d.getWindow().findViewById(R.id.design_bottom_sheet);

                CoordinatorLayout.LayoutParams params = ((CoordinatorLayout.LayoutParams) content.getLayoutParams());
                params.setMargins(getResources().getDimensionPixelSize(R.dimen.fixed_padding_large), 0 ,getResources().getDimensionPixelSize(R.dimen.fixed_padding_large), getResources().getDimensionPixelSize(R.dimen.fixed_padding_large));
                content.setLayoutParams(params);

            }
        });
        return d;
    }

Try it from your layout design wrap your content with a parent layout and set parent layout background transparent then on your child set margin bottom

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <androidx.cardview.widget.CardView
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:cardCornerRadius="10dp"
        app:cardBackgroundColor="#FFFFFF">

    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM