![](https://imgsw.cn/static/images/BottomSheetDialog.png)
MD 风格的底部弹窗,比自定义 Dialog 或 PopupWindow 使用更简单,功能也更强大,比如可以方便的实现拖拽关闭。 细分来说,分为 BottomSheet、BottomSheetDialog、BottomSheetDialogFragment。
- BottomSheet:依赖于 CoordinatorLayout 和 BottomSheetBehavior,需要将底部菜单作为CoordinatorLayout 的子 View,并且有三个关键的属性需要其设置。
- app:layout_behavior="@string/bottom_sheet_behavior":代表这是一个 Bottom Sheet。
- app:behavior_peekHeight=“66dp”:当关闭时,底部能看到的高度,不显示的话设置为 0 即可。
- app:behavior_hideable=“true”:当拖拽下拉时,是否能全部隐藏。
- BottomSheetDialog:不必指定父布局是 CoordinatorLayout。因为在 setContentView 时,自定义布局也是会自动被 CoordinatorLayout 包裹起来。
- BottomSheetDialogFragment:通过继承 BottomSheetFragment 来实现底部菜单布局,并且根据 Fragment 的生命周期做较多逻辑操作的情况。
BottomSheetDialog
使用起来更灵活。父布局与底部菜单布局正常写即可。
示例:
父布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/plot"