Android BottomSheetDialogFragment底部弹出栏的几种用法(圆角、禁止滑动收起)
<style name="BottomSheetDialogStyle" parent="Theme.Design.BottomSheetDialog"><item name="android:windowFrame">@null</item><item name="android:windowIsFloatin...
·
1.引入
implementation 'com.android.support:design:29.1.0'
2.简单用法
- BottomDialogFragment1其中R.layout.fragment_bottom_dialog 是dialog的布局,父布局的layout_height需要设置成wrap_content。里面的子布局可以设置高度。
package com.zhangyu.bottomsheetdialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
public class BottomDialogFragment1 extends BottomSheetDialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bottom_dialog, container, false);
initView(view);
return view;
}
private void initView(View view) {
}
}
- 调用的地方
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_test_1:
new BottomDialogFragment1().show(getSupportFragmentManager(),"BottomDialogFragment1");
break;
}
}
}
3.设置圆角
- 圆角的shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF402416" />
<corners
android:topLeftRadius="16dp"
android:topRightRadius="16dp" />
</shape>
- 设置默认背景透明(解决设置圆角时默认背景白色问题)
<style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<!--动画设置-->
<item name="android:windowAnimationStyle">@style/Animation.Design.BottomSheetDialog</item>
</style>
- BottomDialogFragment2中,在onCreateDialog中设置setStyle(STYLE_NO_TITLE, R.style.BottomDialog);
public class BottomDialogFragment2 extends BottomSheetDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setStyle(STYLE_NO_TITLE, R.style.BottomDialog);
return super.onCreateDialog(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bottom_dialog, container, false);
initView(view);
return view;
}
private void initView(View view) {
}
}
4.禁止滑动收起底部栏
- onCreateView中增加了一个 BottomSheetBehavior behavior=BottomSheetBehavior.from(getDialog().findViewById(R.id.design_bottom_sheet));
通过behavior来控制隐藏行为 - 需要在post中进行获取getDialog否则获取不到null。java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.app.Dialog.findViewById(int)’ on a null object reference
public class BottomDialogFragment3 extends BottomSheetDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setStyle(STYLE_NO_TITLE, R.style.BottomDialog);
return super.onCreateDialog(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bottom_dialog, container, false);
view.post(new Runnable() {
@Override
public void run() {
//R.id.design_bottom_sheet基本是固定的,不用担心后面API的更改
BottomSheetBehavior behavior=BottomSheetBehavior.from(getDialog().findViewById(R.id.design_bottom_sheet));
behavior.setHideable(false);//此处设置表示禁止BottomSheetBehavior的执行
}
});
initView(view);
return view;
}
private void initView(View view) {
}
}
5.传递参数重写show方法
public void show(@NonNull FragmentManager manager, @Nullable String tag, String userName, String industry) {
super.show(manager, tag);
this.userName = userName;
this.industry = industry;
}
设置弹窗的弹出高度
binding.root.post {
//如果是小屏手机
if (ScreenUtils.getAppScreenHeight() <= 1920) {
val behavior = BottomSheetBehavior.from(requireDialog().findViewById(R.id.design_bottom_sheet))
behavior.peekHeight = binding.airPanelMainLayout.measuredHeight
}
}
侧边弹出栏
更多推荐
所有评论(0)