Android学习之BottomSheetDialog组件的使用
作者:JulyYu 时间:2023-12-10 07:58:30
基本介绍
BottomSheetDialog
是底部操作控件,可在屏幕底部创建一个支持滑动关闭视图。
目前依赖使用如下:
implementation 'com.google.android.material:material:1.4.0'
基础使用
BottomSheetDialog
需要为它添加视图内容,类似Dialog
,且BottomSheetDialog
的高度由自定义视图决定。
var text = TextView(this@UIBottomSheetAC)
text.text = "BottomSheetDialog"
var linearLayout = LinearLayout(this@UIBottomSheetAC)
linearLayout.addView(text)
linearLayout.setBackgroundColor(Color.YELLOW)
linearLayout.layoutParams = LinearLayout.LayoutParams(-1,500)
val bottomSheetDialog =
BottomSheetDialog(context, R.style.bottom_sheet_dialog)
bottomSheetDialog.setContentView(linearLayout)
bottomSheetDialog.show()
其他功能实现
圆角样式实现
BottomSheetDialog
官方默认样式是矩形弹窗并不带圆角设置。但在日常开发中会遇到需要圆角弹窗设计要求需要对BottomSheetDialog
默认样式做一些调整才能实现。
BottomSheetDialog样式文件
<style name="bottom_sheet_dialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottom_sheet_style_wrapper</item>
</style>
<style name="bottom_sheet_style_wrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
布局背景圆角
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_blue_light" />
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
代码配置
// 视图背景增加圆角样式
linearLayout.background = getDrawable(R.drawable.ui_shape_top_radius15)
// bottomSheetDialog设置透明背景样式
val bottomSheetDialog =
BottomSheetDialog(context, R.style.bottom_sheet_dialog)
去弹窗外部遮罩阴影
增加android:backgroundDimEnabled
属性为false实现无背景阴影遮罩效果。
<style name="bottom_sheet_dialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottom_sheet_style_wrapper</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
带阴影
不带阴影
关闭触发设置
是否支持拖拽关闭通过设置
setCancelable
方法实现。是否支持点击视图外部关闭弹窗通过
setCanceledOnTouchOutside
方法实现
bottomSheetDialog.setCancelable(false)
bottomSheetDialog.setCanceledOnTouchOutside(true)
列表视图使用
使用列表功能也是可以直接实现,添加ListView
即可,列表高度可设置ViewGroup.LayoutParams
实现(默认情况下若列表数据较多会撑满整个屏幕)。
Button(this).run {
it.addView(this)
text = "BottomSheetListDialog"
setOnClickListener {
var listView = ListView(this@UIBottomSheetAC)
listView.adapter =
ArrayAdapter<String>(
this@UIBottomSheetAC,
android.R.layout.simple_list_item_1,
values
)
var coordinatorLayout = CoordinatorLayout(this@UIBottomSheetAC)
val params = ViewGroup.LayoutParams(
resources.displayMetrics.widthPixels,
resources.displayMetrics.heightPixels
)
coordinatorLayout.addView(listView)
val bottomSheetDialog =
BottomSheetDialog(context, R.style.bottom_sheet_dialog)
bottomSheetDialog.setContentView(coordinatorLayout,params)
bottomSheetDialog.show()
}
}
但使用BottomSheetBehavior
要求根布局必须是CoordinatorLayout
否则会报错。
val bottomSheetBehavior = BottomSheetBehavior.from(coordinatorLayout)
bottomSheetBehavior.peekHeight = resources.displayMetrics.heightPixels * 3 / 4
bottomSheetBehavior.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetDialog.dismiss()
}
}
})
来源:https://juejin.cn/post/7111350570140565512
标签:Android,BottomSheetDialog
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android日历控件PickTime代码实例
2021-09-28 17:22:57
![](https://img.aspxhome.com/file/2023/4/106044_0s.png)
Java中new关键字和newInstance方法的区别分享
2022-12-04 03:12:52
Android实现计时与倒计时的方法汇总
2023-11-09 19:29:28
![](https://img.aspxhome.com/file/2023/8/94968_0s.png)
C#中使用IFormattable实现自定义格式化字符串输出示例
2023-05-31 23:34:04
![](https://img.aspxhome.com/file/2023/6/104226_0s.png)
C#中Socket与Unity相结合示例代码
2022-09-15 23:24:15
C# [ImportDll()] 知识小结
2022-09-15 07:42:18
详解SpringMVC实现图片上传以及该注意的小细节
2022-04-29 08:34:30
JAVA递归生成树形菜单的实现过程
2023-07-15 08:57:22
![](https://img.aspxhome.com/file/2023/2/57712_0s.png)
详解Java单元测试之JUnit篇
2023-06-12 16:42:13
![](https://img.aspxhome.com/file/2023/1/67491_0s.jpg)
Java实现inputstream流的复制代码实例
2021-08-30 11:52:11
关于mybatis3中@SelectProvider的使用问题
2021-11-20 17:03:19
Java基础入门总结之序列化和反序列化
2023-02-19 03:29:10
C#集合之链表的用法
2022-02-07 13:38:34
![](https://img.aspxhome.com/file/2023/2/108792_0s.jpg)
Java JNDI案例详解
2022-06-09 07:56:35
Spring Cloud Gateway不同频率限流的解决方案(每分钟,每小时,每天)
2023-01-05 13:49:34
Java内存溢出案例模拟和原理分析过程
2023-04-09 07:27:48
![](https://img.aspxhome.com/file/2023/3/125573_0s.png)
c# 中文转拼音without CJK
2023-06-08 23:59:31
Java编程Nashorn实例代码
2022-07-30 05:28:21
详解从ObjectPool到CAS指令
2022-04-26 16:38:05
![](https://img.aspxhome.com/file/2023/6/91296_0s.png)
浅谈java异常链与异常丢失
2023-10-24 21:53:38