Android學習之BottomSheetDialog組件的使用
基本介紹
BottomSheetDialog是底部操作控件,可在屏幕底部創(chuàng)建一個支持滑動關閉視圖。
目前依賴使用如下:
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官方默認樣式是矩形彈窗并不帶圓角設置。但在日常開發(fā)中會遇到需要圓角彈窗設計要求需要對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>

帶陰影

不帶陰影
關閉觸發(fā)設置
- 是否支持拖拽關閉通過設置
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()
}
}
})到此這篇關于Android學習之BottomSheetDialog組件的使用的文章就介紹到這了,更多相關Android BottomSheetDialog內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android長按imageview把圖片保存到本地的實例代碼
本文通過代碼給大家介紹了Android長按imageview把圖片保存到本地的實現方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-12-12
Android開發(fā)實現帶有反彈效果仿IOS反彈scrollview教程詳解
本文給大家分享android開發(fā)實現帶有反彈效果,模仿ios反彈scrollview詳細教程,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09

