: BottomSheetDialogFragment() 内重写getTheme方法
override fun getTheme(): Int {
return R.style.CustomBottomSheetDialogTheme
}
<style name="CustomBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
<!-- 背景不带阴影-->
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="CustomBottomSheetStyle" parent="Widget.MaterialComponents.BottomSheet">
<item name="android:background">@drawable/rounded_bottom_sheet</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:topLeftRadius="16dp"
android:topRightRadius="16dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"/>
</shape>
onCreateDialog 中
dialog.setOnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
// 设置圆角背景
// bottomSheet?.background = ContextCompat.getDrawable(requireContext(), R.drawable.rounded_bottom_sheet_with_shadow)
// 创建一个带有圆角和阴影的背景
val radius = resources.getDimension(R.dimen.dimens_16dp)
val shapeAppearanceModel =
ShapeAppearanceModel.builder().setTopLeftCorner(CornerFamily.ROUNDED, radius)
.setTopRightCorner(CornerFamily.ROUNDED, radius).build()
val materialShapeDrawable = MaterialShapeDrawable(shapeAppearanceModel).apply {
fillColor = ColorStateList.valueOf(
ContextCompat.getColor(
requireContext(), android.R.color.white
)
)
elevation = resources.getDimension(R.dimen.dimens_16dp)
shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
}
// 设置圆角背景和阴影
bottomSheet?.background = materialShapeDrawable
if (isExpand) {
// 完全展开
BottomSheetBehavior.from(bottomSheet!!).state = BottomSheetBehavior.STATE_EXPANDED
} else {
BottomSheetBehavior.from(bottomSheet!!).state = BottomSheetBehavior.STATE_COLLAPSED
}
}