Android BottomSheet总结
创始人
2024-01-25 04:49:54
0

文章目录

  • Android BottomSheet总结
    • BottomSheet
    • BottomSheetDialog
    • BottomSheetDialogFragment
    • 全屏无阴影BottomSheetDialogFragment
    • 代码下载

Android BottomSheet总结

BottomSheet

请添加图片描述

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) {}
})

BottomSheetDialog

在这里插入图片描述

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()

BottomSheetDialogFragment

在这里插入图片描述

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")
}

全屏无阴影BottomSheetDialogFragment

在这里插入图片描述

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")
}

代码下载

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...