網站首頁 編程語言 正文
基本介紹
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
相關推薦
- 2022-09-02 Python中的Numpy?面向數組編程常見操作_python
- 2022-06-12 PostgreSQL數據庫事務插入刪除及更新操作示例_PostgreSQL
- 2022-07-10 ENOENT讀取文件報錯(fs)
- 2022-01-11 小程序 ------ [mini-css-extract-plugin] css沖突問題
- 2023-01-20 Flask框架使用異常捕獲問題_python
- 2022-09-21 詳解C語言中typedef和#define的用法與區別_C 語言
- 2022-11-27 關于HTTPS端口443的技術講解(什么是443端口)_服務器其它
- 2024-01-14 “xxx“ is not an enclosing class 解決辦法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支