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样式:
<style name="MyBottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
- "bottomSheetStyle"
>@style/bottomSheetStyleWrapper
style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
- "android:background"
>@android:color/transparent
style>
dialog_bottom_sheet布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_shape"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="拍照" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="相册" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="取消"
android:textColor="@color/red" />
LinearLayout>
代码:
val bottomSheetDialog = BottomSheetDialog(mContext, R.style.MyBottomSheetDialogStyle)
bottomSheetDialog.dismissWithAnimation
bottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet)
val cancel: TextView = bottomSheetDialog.findViewById<TextView>(R.id.cancel)!!
cancel.setOnClickListener {
bottomSheetDialog.dismiss()
}
bottomSheetDialog.show()
MyBottomSheetDialog代码:
class MyBottomSheetDialog : BottomSheetDialogFragment() {
private lateinit var cancel: TextView
override 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样式:
<style name="MyBottomSheetDialogBgStyle" parent="Theme.Design.Light.BottomSheetDialog">
- "bottomSheetStyle"
>@style/bottomSheetStyleWrapper
- "android:backgroundDimEnabled"
>false
style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
- "android:background"
>@android:color/transparent
style>
dialog_full布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_shape"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/iv_close"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_close" />
RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入"
android:inputType="text"
android:padding="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/desc" />
LinearLayout>
MyFullDialog代码:
class MyFullDialog : BottomSheetDialogFragment() {
private lateinit var close: ImageView
override 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")
}