BottomSheetDialogFragment 视图仅在设备旋转后才变为透明

BottomSheetDialogFragment view becomes transparent only after device rotation

我想创建 BottomSheetDialogFragment 圆角。

我为实现这一目标所做的工作:

    <style name="AppTheme.TransparentBottomSheetStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
        <item name="android:background">@android:color/transparent</item>
    </style>

    <style name="AppTheme.BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.Dialog">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:colorBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="bottomSheetStyle">@style/AppTheme.TransparentBottomSheetStyle</item>
    </style>

backdrop_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#00FF00"/>

    <corners
            android:topLeftRadius="24dp"
            android:topRightRadius="24dp"/>
</shape>

底部sheet布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/backdrop_shape"
        android:paddingBottom="18dp">

和代码:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.AppTheme_BottomSheetDialogTheme)
    dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}

这适用于 AppCompat 主题,但不适用于 MaterialComponents:

如您所见,有白色背景(我想,它是 id design_bottom_sheet 的父 FrameLayout)。

最有趣的是,当我旋转设备(然后将其旋转回来)时,它变成了我想要的透明:

这是错误还是功能?如何解决?

注意: 我已经尝试了所有发布在 SO 和其他网站上的解决方案,它们也不适用于 MaterialComponents

根据DialogFragment.setStyle

Call to customize the basic appearance and behavior of the fragment's dialog. This can be used for some common dialog behaviors, taking care of selecting flags, theme, and other options for you. The same effect can be achieve by manually setting Dialog and Window attributes yourself. Calling this after the fragment's Dialog is created will have no effect.

我猜 setStyle 应该在 onCreate 之前或之前被调用。