主要通过styles.xml设置样式来实现。
下面是styles.xml的部分:
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/shape_sheet_dialog_bg</item>
</style>
通过将android:backgroundDimEnabled
(dim是昏暗的意思)(总的意思就是背景是否设置为昏暗模式),默认是true
,现在我们设为false
。
本来想通过更改android:background
属性来实现圆角矩形的。
@drawable/shape_sheet_dialog_bg
的内容是:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:topLeftRadius="16dp"
android:topRightRadius="16dp"/>
</shape>
效果如下图:
java代码创建BottomSheetDialog时,指定此时的style。如下:
final BottomSheetDialog bottomSheetDialog = new
BottomSheetDialog(this,R.style.AppBottomSheetDialogTheme);
实现的效果变化如下图:
之前:
之后:
但是圆角的效果并没有实现,欢迎大神在评论区解答我的疑惑,为啥圆角的效果没有实现。