bottomSheetDialogFragment full screen
我想要实现的是类似于Instagram应用内网络浏览器的功能,该功能可在您点击广告时使用:
我所做的是我使用了WebView bottomSheetDialogFragment,并且重写了
1 2 3 4 5 6 7 8 9 10 11 12 | @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState); bottomSheetDialog.setOnShowListener(dialog -> { BottomSheetDialog dialogc = (BottomSheetDialog) dialog; FrameLayout bottomSheet = dialogc .findViewById(android.support.design.R.id.design_bottom_sheet); BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED); //BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true); //BottomSheetBehavior.from(bottomSheet).setHideable(true); }); return bottomSheetDialog; } |
这是我得到的结果:
我的问题是,如何获得全屏效果,或者如何实现instagram浏览器之类的功能?
ps:我尝试了第一个chrome自定义标签,但无法在对话框片段中添加它。
谢谢。
在您的自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialogInterface) { BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface; setupFullHeight(bottomSheetDialog); } }); return dialog; } private void setupFullHeight(BottomSheetDialog bottomSheetDialog) { FrameLayout bottomSheet = (FrameLayout) bottomSheetDialog.findViewById(R.id.design_bottom_sheet); BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams(); int windowHeight = getWindowHeight(); if (layoutParams != null) { layoutParams.height = windowHeight; } bottomSheet.setLayoutParams(layoutParams); behavior.setState(BottomSheetBehavior.STATE_EXPANDED); } private int getWindowHeight() { // Calculate window height for fullscreen use DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.heightPixels; } |
您可以通过将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState); bottomSheetDialog.setOnShowListener(dialog -> { BottomSheetDialog dialogc = (BottomSheetDialog) dialog; // When using AndroidX the resource can be found at com.google.android.material.R.id.design_bottom_sheet FrameLayout bottomSheet = dialogc.findViewById(android.support.design.R.id.design_bottom_sheet); BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); bottomSheetBehavior.setPeekHeight(Resources.getSystem().getDisplayMetrics().heightPixels); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); }); return bottomSheetDialog; } |
建议的解决方案基于内部
我的解决方案将布局高度设置为
1 2 3 4 5 | override fun onStart() { super.onStart() val sheetContainer = requireView().parent as? ViewGroup ?: return sheetContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT } |
如果看一下
这样,您无需依赖ID。 我使用的是
如果您从一开始就需要全高(最有可能)
1 2 3 4 5 6 | override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return BottomSheetDialog(requireContext(), theme).apply { behavior.state = BottomSheetBehavior.STATE_EXPANDED behavior.peekHeight = //your harcoded or dimen height } } |