Android學(xué)習(xí)之BottomSheetDialog組件的使用
基本介紹
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è)置。但在日常開發(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)閉通過設(shè)置
setCancelable
方法實(shí)現(xiàn)。 - 是否支持點(diǎn)擊視圖外部關(guān)閉彈窗通過
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() } } })
到此這篇關(guān)于Android學(xué)習(xí)之BottomSheetDialog組件的使用的文章就介紹到這了,更多相關(guān)Android BottomSheetDialog內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義一個(gè)view?ViewRootImpl繪制流程示例
這篇文章主要為大家介紹了Android自定義一個(gè)view?ViewRootImpl繪制流程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android入門之PopupWindow用法實(shí)例解析
這篇文章主要介紹了Android入門之PopupWindow用法,對(duì)于Android初學(xué)者來(lái)說(shuō)有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08Android 自定義布局豎向的ViewPager的實(shí)現(xiàn)
這篇文章主要介紹了Android 自定義布局豎向的ViewPager的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05利用Android實(shí)現(xiàn)比較炫酷的自定義View
自定義View、多線程、網(wǎng)絡(luò),被認(rèn)為是Android開發(fā)者必須牢固掌握的最基礎(chǔ)的三大基本功,這篇文章主要給大家介紹了關(guān)于如何利用Android實(shí)現(xiàn)比較炫酷的自定義View的相關(guān)資料,需要的朋友可以參考下2021-07-07解決java.lang.NoClassDefFoundError: android.support.v4.animati
這篇文章主要介紹了解決Android Studio出現(xiàn)java.lang.NoClassDefFoundError: android.support.v4.animation.AnimatorCompatHelper的問題,感興趣的朋友一起看看吧2021-08-08android平臺(tái)HttpGet、HttpPost請(qǐng)求實(shí)例
出自網(wǎng)絡(luò)搜索引擎巨頭的Android平臺(tái),其對(duì)網(wǎng)絡(luò)的支持自然不用多說(shuō),在Android SDK中已經(jīng)集成了Apache的HttpClient模塊。使用HttpClient模塊,我們就可以使用HTTP協(xié)議進(jìn)行網(wǎng)絡(luò)連接了2014-05-05Android中MPAndroidChart自定義繪制最高點(diǎn)標(biāo)識(shí)的方法
目前在做一款軟件,要求在展示走勢(shì)圖的時(shí)候?qū)ψ罡唿c(diǎn)進(jìn)行自定義繪制,下面這篇文章主要給大家介紹了關(guān)于Android中MPAndroidChart自定義繪制最高點(diǎn)標(biāo)識(shí)的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03Android學(xué)習(xí)之Intent中顯示意圖和隱式意圖的用法實(shí)例分析
這篇文章主要介紹了Android學(xué)習(xí)之Intent中顯示意圖和隱式意圖的用法,以實(shí)例形式分析了Intent通訊的相關(guān)技巧與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Kotlin類與屬性及構(gòu)造函數(shù)的使用詳解
這篇文章主要介紹了Kotlin語(yǔ)言中類與屬性及構(gòu)造函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09