首先要先创建一个类,然后继承:BottomSheetDialogFragment 重写 onCreateDialog 方法;
- public class CommentBottomDialog extends BottomSheetDialogFragment {
-
- @NonNull
- @NotNull
- @Override
- public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
- if (getActivity() == null) return super.onCreateDialog(savedInstanceState);
- // 第二个参数是设置 dialog 的背景样式
- BottomSheetDialog dialog = new BottomSheetDialog(getActivity(), R.style.BottomSheetStyle);
- // 这个是设置 dialog 弹出动画
- Window window = dialog.getWindow();
- if (window != null) {
- window.setWindowAnimations(R.style.BottomSheetStyle);
- }
-
- // 设置 dialog 布局
- initView(dialog);
-
- return dialog;
- }
-
- private void initView(BottomSheetDialog dialog) {
- View root = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_comment_layout, null);
- dialog.setContentView(root);
- //设置高度
- ViewGroup.LayoutParams params = root.getLayoutParams();
- params.height = (int) (0.75 *
- getResources().getDisplayMetrics().heightPixels);
- root.setLayoutParams(params);
-
- // 初始化 dialog 布局里的控件
- ImageView iv_close = root.findViewById(R.id.iv_close);
- // 点击关闭 dialog
- iv_close.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- dialog.dismiss();
- }
- });
-
- // TODO 下面做一些其他操作自由扩展
- // ...
-
- }
-
- }
BottomSheetStyle
-
- <style name="BottomSheetStyle" parent="Theme.AppCompat.Dialog">
-
- <item name="android:windowBackground">@android:color/transparentitem>
-
- <item name="android:windowFrame">@nullitem>
-
- <item name="android:backgroundDimEnabled">trueitem>
-
- <item name="android:windowIsFloating">trueitem>
-
- <item name="android:windowEnterAnimation">@anim/popup_initem>
- <item name="android:windowExitAnimation">@anim/popup_outitem>
- style>
在res文件夹下新建文件夹:anim
popup_in
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="400"/>
- <alpha android:fromAlpha="0" android:toAlpha="1.0" android:duration="400"/>
- set>
popup_out
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="500"/>
- <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="500"/>
- set>
- CommentBottomDialog bottomDialog = new CommentBottomDialog();
- bottomDialog.show(getBaseActivity().getSupportFragmentManager(), "halo");