方法1 BottomSheetBehavior
#xml
#app:behavior_hideable="true" 这个不用会闪退
#app:behavior_peekHeight弹出高度
<?xml version="1.0" encoding="utf-8"?>
< androidx.coordinatorlayout.widget.CoordinatorLayout
...
<LinearLayout
android: id= " @+id/meter_bottom_menu"
android: layout_width= " match_parent"
android: layout_height= " wrap_content"
app: behavior_hideable= " true"
app: layout_behavior= " @string/bottom_sheet_behavior"
android: orientation= " vertical" >
...
</ androidx.coordinatorlayout.widget.CoordinatorLayout>
private BottomSheetBehavior < LinearLayout > behavior;
. . .
LinearLayout bottomMenu = findViewById ( R . id. meter_bottom_menu) ;
behavior = BottomSheetBehavior . from ( bottomMenu) ;
behavior. setState ( BottomSheetBehavior . STATE_HIDDEN) ;
behavior. setBottomSheetCallback ( new BottomSheetBehavior. BottomSheetCallback ( ) {
@Override
public void onStateChanged ( @NonNull View view, int i) {
}
@Override
public void onSlide ( @NonNull View view, float v) {
}
} ) ;
. . .
if ( behavior. getState ( ) == BottomSheetBehavior . STATE_HIDDEN) {
behavior. setState ( BottomSheetBehavior . STATE_COLLAPSED) ;
} else {
behavior. setState ( BottomSheetBehavior . STATE_HIDDEN) ;
}
方法2 BottomSheetDialog
val view = View . inflate ( this , R . layout. bottom_sheet_dialog1, null )
. . . . . .
val dialog = BottomSheetDialog ( this )
dialog. setContentView ( view)
val behavior = BottomSheetBehavior . from ( view. parent as View )
behavior. state = BottomSheetBehavior . STATE_HALF_EXPANDED
dialog. show ( )