Android學(xué)習(xí)之BottomSheetDialog組件的使用
基本介紹
BottomSheetDialog
是底部操作控件,可在屏幕底部創(chuà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()
其他功能實現(xiàn)
圓角樣式實現(xiàn)
BottomSheetDialog
官方默認(rèn)樣式是矩形彈窗并不帶圓角設(shè)置。但在日常開發(fā)中會遇到需要圓角彈窗設(shè)計要求需要對BottomSheetDialog
默認(rèn)樣式做一些調(diào)整才能實現(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實現(xiàn)無背景陰影遮罩效果。
<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
方法實現(xiàn)。 - 是否支持點擊視圖外部關(guān)閉彈窗通過
setCanceledOnTouchOutside
方法實現(xiàn)
bottomSheetDialog.setCancelable(false) bottomSheetDialog.setCanceledOnTouchOutside(true)
列表視圖使用
使用列表功能也是可以直接實現(xiàn),添加ListView
即可,列表高度可設(shè)置ViewGroup.LayoutParams
實現(xiàn)(默認(rèn)情況下若列表數(shù)據(jù)較多會撐滿整個屏幕)。
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() } } })
到此這篇關(guān)于Android學(xué)習(xí)之BottomSheetDialog組件的使用的文章就介紹到這了,更多相關(guān)Android BottomSheetDialog內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義一個view?ViewRootImpl繪制流程示例
這篇文章主要為大家介紹了Android自定義一個view?ViewRootImpl繪制流程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android 自定義布局豎向的ViewPager的實現(xiàn)
這篇文章主要介紹了Android 自定義布局豎向的ViewPager的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05解決java.lang.NoClassDefFoundError: android.support.v4.animati
這篇文章主要介紹了解決Android Studio出現(xiàn)java.lang.NoClassDefFoundError: android.support.v4.animation.AnimatorCompatHelper的問題,感興趣的朋友一起看看吧2021-08-08Android中MPAndroidChart自定義繪制最高點標(biāo)識的方法
目前在做一款軟件,要求在展示走勢圖的時候?qū)ψ罡唿c進(jìn)行自定義繪制,下面這篇文章主要給大家介紹了關(guān)于Android中MPAndroidChart自定義繪制最高點標(biāo)識的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03Android學(xué)習(xí)之Intent中顯示意圖和隱式意圖的用法實例分析
這篇文章主要介紹了Android學(xué)習(xí)之Intent中顯示意圖和隱式意圖的用法,以實例形式分析了Intent通訊的相關(guān)技巧與注意事項,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Kotlin類與屬性及構(gòu)造函數(shù)的使用詳解
這篇文章主要介紹了Kotlin語言中類與屬性及構(gòu)造函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09