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