網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
基本介紹
BottomSheetDialog
是底部操作控件,可在屏幕底部創(chuàng)建一個(gè)支持滑動(dòng)關(guān)閉視圖。
目前依賴使用如下:
implementation 'com.google.android.material:material:1.4.0'
基礎(chǔ)使用
BottomSheetDialog
需要為它添加視圖內(nèi)容,類似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()
其他功能實(shí)現(xiàn)
圓角樣式實(shí)現(xiàn)
BottomSheetDialog
官方默認(rèn)樣式是矩形彈窗并不帶圓角設(shè)置。但在日常開(kāi)發(fā)中會(huì)遇到需要圓角彈窗設(shè)計(jì)要求需要對(duì)BottomSheetDialog
默認(rèn)樣式做一些調(diào)整才能實(shí)現(xiàn)。
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設(shè)置透明背景樣式 val bottomSheetDialog = BottomSheetDialog(context, R.style.bottom_sheet_dialog)
去彈窗外部遮罩陰影
增加android:backgroundDimEnabled
屬性為false實(shí)現(xiàn)無(wú)背景陰影遮罩效果。
<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>
帶陰影
不帶陰影
關(guān)閉觸發(fā)設(shè)置
- 是否支持拖拽關(guān)閉通過(guò)設(shè)置
setCancelable
方法實(shí)現(xiàn)。 - 是否支持點(diǎn)擊視圖外部關(guān)閉彈窗通過(guò)
setCanceledOnTouchOutside
方法實(shí)現(xiàn)
bottomSheetDialog.setCancelable(false) bottomSheetDialog.setCanceledOnTouchOutside(true)
列表視圖使用
使用列表功能也是可以直接實(shí)現(xiàn),添加ListView
即可,列表高度可設(shè)置ViewGroup.LayoutParams
實(shí)現(xiàn)(默認(rèn)情況下若列表數(shù)據(jù)較多會(huì)撐滿整個(gè)屏幕)。
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
否則會(huì)報(bào)錯(cuò)。
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
相關(guān)推薦
- 2022-12-28 jquery實(shí)現(xiàn)點(diǎn)擊瀏覽器返回上一頁(yè)按鈕并能直接刷新_jquery
- 2024-01-11 redis開(kāi)啟密碼驗(yàn)證
- 2023-01-02 Kotlin中空判斷與問(wèn)號(hào)和感嘆號(hào)標(biāo)識(shí)符使用方法_Android
- 2022-06-11 python中Event實(shí)現(xiàn)線程間同步介紹_python
- 2023-07-02 Python利用scikit-learn實(shí)現(xiàn)近鄰算法分類的示例詳解_python
- 2022-05-26 Flutter?UI實(shí)現(xiàn)側(cè)拉抽屜菜單_Android
- 2021-12-03 Go語(yǔ)言原子操作及互斥鎖的區(qū)別_Golang
- 2022-05-29 C#獲取攝像頭拍照顯示圖像的方法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支