如何在 bottomsheet 对话框片段中将 scrim 更改为不可见?

How to change scrim to invisible in bottomsheet dialog fragment?

我使用 bottomsheetdialogfragment 实现了 bottomsheet。但默认它具有调光效果(稀松布)。如何移除调光效果(稀松布)或将其更改为不可见,以便我可以清楚地看到其他 UI 元素。

这是我使用的布局。

fragment_bottom_sheet_queue.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/res_bottom_sheet_shape"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp">

        <ImageView
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="10dp"
            android:layout_marginTop="8dp"
            android:background="?selectableItemBackgroundBorderless"
            android:contentDescription="@string/image_description"
            android:padding="10dp"
            android:src="@drawable/ic_close" />

        <TextView
            android:id="@+id/txt_queue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:fontFamily="@font/quicksand_medium"
            android:gravity="center"
            android:text="@string/queue"
            android:textColor="@color/dark_white"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/option"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="?selectableItemBackgroundBorderless"
            android:contentDescription="@string/image_description"
            android:padding="10dp"
            android:src="@drawable/ic_menu" />
        <ImageView
            android:id="@+id/tickoption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:visibility="gone"
            android:background="?selectableItemBackgroundBorderless"
            android:contentDescription="@string/image_description"
            android:padding="10dp"
            android:src="@drawable/ic_done_grey_24dp" />
    </RelativeLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="6dp"
        android:background="@color/light_grey" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/queue_empty_text"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:visibility="gone"
        android:gravity="center"
        android:textSize="16sp"
        android:fontFamily="@font/quicksand_medium"
        android:text="@string/queue_empty_text"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/optionRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@color/screen_background"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />
</LinearLayout>

BottomSheetFragment.java

public class BottomSheetFragment extends BottomSheetDialogFragment {

private BottomSheetBehavior mBehavior;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

        View view = View.inflate(getContext(), R.layout.fragment_bottom_queue_sheet, null);
        activity = getActivity();

        dialog.setContentView(view);
        mBehavior = BottomSheetBehavior.from((View) view.getParent());
        return dialog;
    }

有暗淡效果 (SCRIM)

不调用底部sheet时

您可以更改 Window 标志和暗淡量来实现此效果。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog dialog = super.onCreateDialog(savedInstanceState);

    Window window = dialog.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    return dialog;
}

现在 2021 年可以通过简单的方法更改暗淡量。只需在对话框的 window:

上使用 setDimAmount()
val dialog = BottomSheetDialog(context)
...
dialog.window?.setDimAmount(0f) // remove any dim