当前位置:网站首页>BottomSheetDialog 使用详解,设置圆角、固定高度、默认全屏等
BottomSheetDialog 使用详解,设置圆角、固定高度、默认全屏等
2022-06-11 18:27:00 【InfoQ】
1.效果

底部弹窗dialogpopupwindowBottomSheetBottomSheetDialogBottomSheetDialogFragment2.BottomSheet

同层级高度拉出来XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yechaoa.materialdesign.activity.BottomSheetActivity">
<include
android:id="@+id/include"
layout="@layout/layout_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn_bottom_sheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="BottomSheet"
android:textAllCaps="false" />
...
</LinearLayout>
<LinearLayout
android:id="@+id/ll_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_peekHeight="80dp"
app:layout_behavior="@string/bottom_sheet_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@android:color/holo_red_light"
android:gravity="center"
android:text="上拉解锁隐藏功能"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:text="a"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@android:color/holo_orange_dark"
android:gravity="center"
android:text="b"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:text="c"
android:textSize="20sp" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
- 注意,这里需要协调布局
CoordinatorLayout包裹才行
app:behavior_peekHeight显示高度,不显示的话设置为0即可
app:layout_behavior标示这是一个bottom_sheet
必须代码
btn_bottom_sheet.setOnClickListener {
val behavior = BottomSheetBehavior.from(ll_bottom_sheet)
if (behavior.state == BottomSheetBehavior.STATE_EXPANDED) {
//如果是展开状态,则关闭,反之亦然
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
} else {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
- STATE_COLLAPSED: 折叠状态
- STATE_EXPANDED: 展开状态
- STATE_DRAGGING : 过渡状态
- STATE_SETTLING: 视图从脱离手指自由滑动到最终停下的这一小段时间
- STATE_HIDDEN : 默认无此状态(可通过app:behavior_hideable 启用此状态),启用后用户将能通过向下滑动完全隐藏 bottom sheet
3.BottomSheetDialog

半透明BottomSheetDialog代码
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet)
bottomSheetDialog.show()
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="80dp"
android:paddingBottom="80dp"
android:text="BottomSheetDialog"
android:textSize="30sp"
android:textStyle="bold" />
setContentViewshowclass MyDialog(context: Context) : BottomSheetDialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
4.BottomSheetDialogFragment

BottomSheetDialogDialogFragment代码
class MyBottomSheetDialog : BottomSheetDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
val view = LayoutInflater.from(context).inflate(R.layout.dialog_my_bottom_sheet, null)
dialog.setContentView(view)
initView(view)
return dialog
}
private fun initView(rootView: View) {
//do something
rootView.tv_cancel.setOnClickListener { dismiss() }
}
}
dialogsetContentViewMyBottomSheetDialog().show(supportFragmentManager, "MyBottomSheetDialog")
- FragmentManager
- tag
圆角效果指定高度5.圆角效果
- 先设置原有背景透明
<!--实现BottomSheetDialog圆角效果-->
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
- onCreate中设置style
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.BottomSheetDialog)
}
- 设置我们自己的style
根布局的viewbackgroundandroid:background="@drawable/shape_sheet_dialog_bg"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
<solid android:color="@color/white" />
</shape>
6.去掉背景阴影

backgroundDimEnabledfalse <!--实现BottomSheetDialog圆角效果 且无背景阴影-->
<style name="BottomSheetDialogBg" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
7.设置固定高度

代码
override fun onStart() {
super.onStart()
//拿到系统的 bottom_sheet
val view: FrameLayout = dialog?.findViewById(R.id.design_bottom_sheet)!!
//获取behavior
val behavior = BottomSheetBehavior.from(view)
//设置弹出高度
behavior.peekHeight = 350
}
peekHeight setContentView @Override
public void setContentView(@LayoutRes int layoutResId) {
super.setContentView(wrapInBottomSheet(layoutResId, null, null));
}
wrapInBottomSheet private View wrapInBottomSheet(int layoutResId, @Nullable View view, @Nullable ViewGroup.LayoutParams params) {
ensureContainerAndBehavior();
...
return container;
}
ensureContainerAndBehavior(); /** Creates the container layout which must exist to find the behavior */
private FrameLayout ensureContainerAndBehavior() {
if (container == null) {
container =
(FrameLayout) View.inflate(getContext(), R.layout.design_bottom_sheet_dialog, null);
FrameLayout bottomSheet = (FrameLayout) container.findViewById(R.id.design_bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.addBottomSheetCallback(bottomSheetCallback);
behavior.setHideable(cancelable);
}
return container;
}
behaviorbehaviorpeekHeight8.设置默认全屏显示
BottomSheetDialogFragment有效高度match_parentdialog?.findViewById(R.id.design_bottom_sheet)!!view.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT

statebehavior.state = BottomSheetBehavior.STATE_EXPANDED

默认位置behavior.peekHeight = 3000
override fun onStart() {
super.onStart()
//拿到系统的 bottom_sheet
val view: FrameLayout = dialog?.findViewById(R.id.design_bottom_sheet)!!
//设置view高度
view.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
//获取behavior
val behavior = BottomSheetBehavior.from(view)
//设置弹出高度
behavior.peekHeight = 3000
//设置展开状态
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}

9.监听展开收起
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
when(newState){
BottomSheetBehavior.STATE_EXPANDED->{}
BottomSheetBehavior.STATE_COLLAPSED->{}
BottomSheetBehavior.STATE_DRAGGING->{}
BottomSheetBehavior.STATE_SETTLING->{}
BottomSheetBehavior.STATE_HIDDEN->{}
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
}
})
10.Github
BottomSheetDialog边栏推荐
- Niu Ke brushes the question - no two
- Force buckle 34 finds the first and last positions of elements in a sorted array
- Niu Ke's questions -- binary search tree and bidirectional linked list
- 全志T3开发板(4核ARM Cortex-A7)——系统启动阶段LOGO显示详解
- Tips for using apipost
- Combination sum of 39 questions
- 力扣34在排序数组中查找元素的第一个和最后一个位置
- Niuke brush questions part6
- 动态爆炸效果
- Dynamic explosion effect
猜你喜欢

信号的处理与捕捉

牛客刷题——合法括号序列判断

Gmail:如何撤回发出的邮件?

牛客刷题——把字符串转换成整数

全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院
開發中必備的文件的上傳與下載

VIM common commands
Téléchargement et téléchargement des fichiers nécessaires au développement

Niu Ke's questions -- binary search tree and bidirectional linked list

Signal processing and capture
随机推荐
添加自己喜欢的背景音乐
Easycwmp source code analysis
User group actions
labelme进行图片数据标注
高性能架构设计
Tips for using apipost
牛客刷题——合法括号序列判断
学习使用LSTM和IMDB评论数据进行情感分析训练
Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case
全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院
Function and principle of key in V-for
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
系统的可扩展型
Niu Ke's questions -- binary search tree and bidirectional linked list
[c language] output the students within the specified score range with the structure
信号的处理与捕捉
Mysql从0到1的完全深入学习--阶段二---基础篇
初识企业级平台
公共字段自动填充,你了解吗
Non recursive traversal of binary tree