XML布局:
CoordinatorLayout
布局behavior_peekHeight
属性表示默认显示高度,设置为0则不显示layout_behavior
属性代码:
val behavior = BottomSheetBehavior.from(llBottom)
btnBottomSheet.setOnClickListener {if (behavior.state == BottomSheetBehavior.STATE_EXPANDED) {behavior.state = BottomSheetBehavior.STATE_COLLAPSED} else if (behavior.state == BottomSheetBehavior.STATE_COLLAPSED) {behavior.state = BottomSheetBehavior.STATE_EXPANDED}
}
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {override fun onStateChanged(bottomSheet: View, newState: Int) {if (newState == BottomSheetBehavior.STATE_EXPANDED) {tvTitle.text = "下滑收起"} else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {tvTitle.text = "上拉展开"}}override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
MyBottomSheetDialogStyle样式:
dialog_bottom_sheet布局:
代码:
val bottomSheetDialog = BottomSheetDialog(mContext, R.style.MyBottomSheetDialogStyle)
bottomSheetDialog.dismissWithAnimation
bottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet)
val cancel: TextView = bottomSheetDialog.findViewById(R.id.cancel)!!
cancel.setOnClickListener {bottomSheetDialog.dismiss()
}
bottomSheetDialog.show()
MyBottomSheetDialog代码:
class MyBottomSheetDialog : BottomSheetDialogFragment() {private lateinit var cancel: TextViewoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setStyle(STYLE_NORMAL, R.style.MyBottomSheetDialogStyle)}override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View {return inflater.inflate(R.layout.dialog_bottom_sheet, container, false)}override fun onViewCreated(view: View, savedInstanceState: Bundle?) {cancel = view.findViewById(R.id.cancel)cancel.setOnClickListener {dismiss()}}
}
使用:
btnBottomSheetDialogFragment.setOnClickListener {MyBottomSheetDialog().show(supportFragmentManager, "MyBottomSheetDialog")
}
MyBottomSheetDialogBgStyle样式:
dialog_full布局:
MyFullDialog代码:
class MyFullDialog : BottomSheetDialogFragment() {private lateinit var close: ImageViewoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setStyle(STYLE_NORMAL, R.style.MyBottomSheetDialogBgStyle)}override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {return inflater.inflate(R.layout.dialog_full, container, false)}override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)close = view.findViewById(R.id.iv_close)close.setOnClickListener {dismiss()}}override fun onStart() {super.onStart()val view: FrameLayout = dialog?.findViewById(R.id.design_bottom_sheet)!!//设置BottomSheetDialogFragment高度view.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT//设置弹出高度val behavior = BottomSheetBehavior.from(view)behavior.peekHeight = 3000//展开behavior.state = BottomSheetBehavior.STATE_EXPANDED}override fun dismiss() {KeyboardUtils.hideKeyboard(dialog?.currentFocus)super.dismiss()}
}
使用:
btnFullDialog.setOnClickListener {MyFullDialog().show(supportFragmentManager, "MyFullDialog")
}