一、简介
Bottom Sheet是Design Support Library23.2 版本引入的一个类似于对话框的控件。 Bottom Sheet中的内容默认是隐藏起来的,只显示很小一部分,可以通过在代码中设置其状态或者手势操作将其完全展开,或者完全隐藏,或者部分隐藏。
二、使用
1、添加依赖:
implementation 'com.android.support:design:28.0.0'
2、布局
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:andro xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps.MapView android: android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android: android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/height52px" app:behavior_hideable="false" app:behavior_peekHeight="@dimen/height84px" app:layout_behavior="android.support.design.widget.BottomSheetBehavior" tools:ignore="MissingPrefix" android:background="#ffffffff" > <include layout="@layout/bottom_sheet" /> </RelativeLayout> </android.support.design.widget.CoordinatorLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android:layout_width="match_parent" android:layout_height="@dimen/height216px" > <TextView android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:text="bottom_sheet_peek" /> </RelativeLayout>
3、代码实现
//底部抽屉栏展示地址 mBehavior = BottomSheetBehavior.from(mRelativeLayout); mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, @BottomSheetBehavior.State int newState) { String state = "null"; switch (newState) { case 1: state = "STATE_DRAGGING";//过渡状态此时用户正在向上或者向下拖动bottom sheet break; case 2: state = "STATE_SETTLING"; // 视图从脱离手指自由滑动到最终停下的这一小段时间 break; case 3: state = "STATE_EXPANDED"; //处于完全展开的状态 break; case 4: state = "STATE_COLLAPSED"; //默认的折叠状态 break; case 5: state = "STATE_HIDDEN"; //下滑动完全隐藏 bottom sheet break; } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { Log.i("BottomSheetDemo", "slideOffset:" + slideOffset); } });
4、几个属性含义:
// behavior_hideable:定义是否能通过下滑手势收起Bottom Sheet。 app:behavior_hideable="true" // behavior_peekHeight:定义可见部分的高度。 app:behavior_peekHeight="80dp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"