2

我想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>

和底页布局:

<?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

4

1 回答 1

3

根据DialogFragment.setStyle

调用以自定义片段对话框的基本外观和行为。这可用于一些常见的对话行为,为您选择标志、主题和其他选项。自己手动设置Dialog和Window属性也可以达到同样的效果。在创建片段的 Dialog 后调用它不会有任何效果。

我猜也许setStyle应该在或之前调用onCreate

于 2018-11-15T11:37:03.883 回答