一、drawable文件下創建shape文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- rectangle表示爲矩形 -->
<!-- 填充的顏色 -->
<solid android:color="@color/white" />
<!-- 邊框的顏色和粗細 -->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- android:radius 關鍵點,圓角的半徑 -->
<corners
android:radius="2dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp" />
</shape>
- 在設置圓角時,圓角半徑的大小必須大於
1dp
,否則是沒有圓角效果的。 - 如果你想單獨設置某幾個角是圓角, 你必須首先聲明
radius
屬性(必須大於1dp
), 然後在其他四個角的屬性中設置每個角的實際想要的半徑大小, 不想圓角的設置爲0dp
。
將該drawable
文件設爲你BottomSheetDialog
佈局的背景即可。成功則無需第二步,如果發現圓角和背景重疊了則繼續第二步。
二、設置BottomSheetDialog的背景爲透明
style.xml
文件夾下添加下面兩個style
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
- 初始化
BottomSheetDialog
的時候指定上面的style
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this,R.style.BottomSheetDialog);
bottomSheetDialog.setContentView(R.layout.bottom_sheet_dialog);
bottomSheetDialog.show();