Android 全屏

Android 全屏,android,material-design,bottom-sheet,material-components-android,material-components,Android,Material Design,Bottom Sheet,Material Components Android,Material Components,我试图实现的是Instagram in-app web浏览器,当你点击广告时使用: 我所做的是,我使用了一个WebView bottomSheetDialogFragment,我覆盖了onCreateDialog,以获得如下全屏: @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.

我试图实现的是Instagram in-app web浏览器,当你点击广告时使用:

我所做的是,我使用了一个WebView bottomSheetDialogFragment,我覆盖了
onCreateDialog
,以获得如下全屏:

@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自定义选项卡,但我无法将其添加到对话框片段中


谢谢。

您可以通过将
BottomSheetBehavior
peek高度设置为等于
Resources.getSystem().getDisplayMetrics().heightPixels

@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;
}
在自定义中,您可以使用以下内容:

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

建议的解决方案基于使用内部
id
,因为它不是故意公开的,它可能会在没有警告的情况下更改

我的解决方案将布局高度设置为
FrameLayout
,但采用更抽象的方式,因此,即使
ViewGroup
类型发生更改,如果它发生更改,也不会中断

override fun onStart() {
    super.onStart()
    val sheetContainer = requireView().parent as? ViewGroup ?: return
    sheetContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
如果查看
bottomsheet对话框
private View wrapInBottomSheet
方法,您将看到这种情况发生在保证工作表行为的情况下。通过一些额外的调试,我发现片段
视图
FrameLayout
中每个人都在按id查找的直接子项

通过这种方式,您不需要依赖ID。我使用的是
onStart
,因为它是在片段准备好交互时定义的,所以所有的都应该准备好

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


很抱歉回答得太晚,但在自定义BottomSheetDialogFragment中,您可以将
match_parent
设置为底部图纸视图的布局参数,如下所示:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = BottomSheetDialog(requireContext(), theme)
    dialog.setOnShowListener {

        val bottomSheetDialog = it as BottomSheetDialog
        val parentLayout =
            bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
        parentLayout?.let { it ->
            val behaviour = BottomSheetBehavior.from(it)
            setupFullHeight(it)
            behaviour.state = BottomSheetBehavior.STATE_EXPANDED
        }
    }
    return dialog
}

private fun setupFullHeight(bottomSheet: View) {
    val layoutParams = bottomSheet.layoutParams
    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
    bottomSheet.layoutParams = layoutParams
}
override fun onCreateDialog(savedInstanceState:Bundle?):Dialog{
val dialog=BottomSheetDialog(requireContext(),主题)
dialog.setOnShowListener{
val bottomSheetDialog=它作为bottomSheetDialog
val父布局=
bottomSheetDialog.findViewById(com.google.android.material.R.id.design\u bottom\u sheet)
parentLayout?.let{it->
val behavior=BottomSheetBehavior.from(it)
设置全高(it)
behavior.state=BottomSheetBehavior.state\u已展开
}
}
返回对话框
}
私人娱乐设置全高(底图:视图){
val layoutParams=bottomSheet.layoutParams
layoutParams.height=WindowManager.layoutParams.MATCH\u父级
bottomSheet.layoutParams=layoutParams
}

将“高度”设置为“匹配父项”有助于将对话框绘制在导航栏的插图上方

实现这一点的最佳方法是通过XML

通过覆盖默认底部工作表的属性(例如在styles.xml中)自定义默认底部工作表

<style name="Widget.MyApp.BottomSheet.Modal" parent="Widget.MaterialComponents.BottomSheet.Modal">
    <item name="behavior_skipCollapsed">true</item>
    <item name="behavior_fitToContents">true</item>
    <item name="behavior_peekHeight">1000dp</item> // yep, that helped to skip collapsed state at initial
    <item name="behavior_hideable">true</item>
</style>
<style name="Theme.MyApp.BottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/Widget.MyApp.BottomSheet.Modal</item>
</style>

真的
真的
1000dp//是的,这有助于跳过初始时的崩溃状态
真的
进入Widget.MaterialComponents.BottomSheet.Modal,查看可以修改的设置

然后创建从theme.Design.BottomSheetDialog继承的自定义主题,并设置要用自己的样式替代底部图纸的样式。也可以放在styles.xml中

<style name="Widget.MyApp.BottomSheet.Modal" parent="Widget.MaterialComponents.BottomSheet.Modal">
    <item name="behavior_skipCollapsed">true</item>
    <item name="behavior_fitToContents">true</item>
    <item name="behavior_peekHeight">1000dp</item> // yep, that helped to skip collapsed state at initial
    <item name="behavior_hideable">true</item>
</style>
<style name="Theme.MyApp.BottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/Widget.MyApp.BottomSheet.Modal</item>
</style>

@style/Widget.MyApp.BottomSheet.Modal
最后,在活动主题或应用程序主题中定义新创建的底部工作表对话框主题,该主题位于themes.xml中(希望您遵循Google关于打包样式和主题的建议)


... 其他事情太多了
@style/Theme.MyApp.BottomSheetDialog

不适合我。只让我在屏幕上占70%左右。有什么建议吗?@CodeWiget可能是因为底部表单的内容不够大,无法覆盖整个屏幕,如果您需要完整的高度,而不需要状态栏,则应调用:(context as Activity)。findViewById(Window.ID\u ANDROID\u content).heightit的内容视图大小是固定的。那些无法访问
android.support.design.R.id.design\u bottom\u sheet
的用户可以尝试以下方法:
com.google.android.material.R.id.design\u bottom\u sheet
这对我来说是更好的解决方案,当使用AndroidX时,可在com.google.android.material.R.id.design_bottom_工作表上找到资源只需使用R.id.design_bottom_工作表,在我的情况下,无需指定完整的包。很好的解决方案,与预期的一样有效。正确的解决方案: