关于android:bottomSheetDialogFragment全屏

bottomSheetDialogFragment full screen

我想要实现的是类似于Instagram应用内网络浏览器的功能,该功能可在您点击广告时使用:

Instagram web browser

我所做的是我使用了WebView bottomSheetDialogFragment,并且重写了onCreateDialog来获得全屏显示,如下所示:

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;
}

这是我得到的结果:

web browser

我的问题是,如何获得全屏效果,或者如何实现instagram浏览器之类的功能?

ps:我尝试了第一个chrome自定义标签,但无法在对话框片段中添加它。

谢谢。


在您的自定义BottomSheetDialogFragment中,您可以使用类似以下内容的内容:

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;
  }


您可以通过将BottomSheetBehaviorpeekHeight设置为等于Resources.getSystem().getDisplayMetrics().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;
}


建议的解决方案基于内部id的使用,因为它不是故意暴露的,因此可以在没有警告的情况下进行更改。

我的解决方案将布局高度设置为FrameLayout,但以一种更为抽象的方式,因此即使ViewGroup类型更改,它的更改也不会中断。

1
2
3
4
5
override fun onStart() {
    super.onStart()
    val sheetContainer = requireView().parent as? ViewGroup ?: return
    sheetContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}

如果看一下BottomSheetDialogprivate View wrapInBottomSheet方法,您会发现这是为了保证工作表的行为。 一些额外的调试,让我认为片段View是每个人通过id查找的FrameLayout的直接子代。

这样,您无需依赖ID。 我使用的是onStart,因为它是在片段准备进行交互时定义的,因此所有内容都应准备就绪。

如果您从一开始就需要全高(最有可能)

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
    }
}