BottomSheetDialogFragment
使用CoordinatorLayout
。昏暗的背景是一个带有OnClickListener
的简单视图,它关闭对话框,而片移动则由CoordinatorLayout.Behavior
驱动。@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog d = super.onCreateDialog(savedInstanceState);
// view hierarchy is inflated after dialog is shown
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
//this disables outside touch
d.getWindow().findViewById(R.id.touch_outside).setOnClickListener(null);
//this prevents dragging behavior
View content = d.getWindow().findViewById(R.id.design_bottom_sheet);
((CoordinatorLayout.LayoutParams) content.getLayoutParams()).setBehavior(null);
}
});
return d;
}
Kotlin:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val d = super.onCreateDialog(savedInstanceState)
//view hierarchy is inflated after dialog is shown
d.setOnShowListener {
//this disables outside touch
d.window.findViewById<View>(R.id.touch_outside).setOnClickListener(null)
//this prevents dragging behavior
(d.window.findViewById<View>(R.id.design_bottom_sheet).layoutParams as CoordinatorLayout.LayoutParams).behavior = null
}
return d
}
这确实使用了设计库的内部ID,但除非由于某种原因它们被更改,否则应该是稳定的。
R.id.design_bottom_sheet
的更多布局参数,包括底部重力和wrap_content的布局高度,但此时我开始想知道是否应该创建一个带有底部重力和滑入/滑出动画的常规DialogFragment,因为你没有使用Sheet的核心功能,即嵌套滚动、拖动和捕捉。 - Paweldismiss()
,则会抛出IllegalArgumentException
。 - Jemshit IskenderovBottomSheetBehavior
来替换它,该行为不处理/拦截触摸事件。 - Pawel