简体   繁体   English

DialogFragment不占用边框较少的屏幕

[英]DialogFragment does not occupy bezel less screen

I am trying to display a dialog fragment in a bezeless phone which has a notch. 我试图在一个有缺口的bezeless手机中显示一个对话框片段。 Here is the screenshot. 这是截图。

在此输入图像描述

As you can see the dialog fragment does not occupies the whole screen and show an ugly grey color at the top. 如您所见,对话框片段不会占据整个屏幕,并在顶部显示丑陋的灰色。

Here is what i have tried 这是我尝试过的

I am setting the style in DialogFragment 我在DialogFragment中设置样式

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
    }



<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>

I am using the same technique for Activity Screen and it works as it occupies the whole bezelless screen but this does not work for dialog fragment 我使用相同的技术进行活动屏幕,它可以工作,因为它占据整个无边框屏幕,但这不适用于对话框片段

You have two options here. 你有两个选择。 First, let's look at the components that are common to both options; 首先,让我们看看两个选项共有的组件;

DialogFragment's onStart method: DialogFragment的onStart方法:

override fun onStart() {
    super.onStart()
    val dialog: Dialog? = dialog
    if (dialog != null) {
        val width = ViewGroup.LayoutParams.MATCH_PARENT
        val height = ViewGroup.LayoutParams.MATCH_PARENT
        dialog.window?.setLayout(width, height)
        dialog.window?.decorView?.systemUiVisibility =
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_FULLSCREEN
    }
}

DialogFragment's onCreate method: DialogFragment的onCreate方法:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
    }

Dialog xml: Dialog xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#c92145">

    <Button android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Lorem ipsum dolor sit amet"/>

</LinearLayout>

FullScreenDialogStyle: FullScreenDialogStyle:

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

Options: 选项: 选项

Left (Option #1) 左(选项#1)

Add this line to FullScreenDialogStyle 将此行添加到FullScreenDialogStyle

<item name="android:fitsSystemWindows">true</item>

Right (Option #2) 对(选项#2)

Add this line to FullScreenDialogStyle 将此行添加到FullScreenDialogStyle

<item name="android:fitsSystemWindows">false</item>

In your code, you should include android " windowMinWidthMajor " and " windowMinWidthMinor " properties and set dialog.getWindow() layouts to MATCH_PARENT and make the layout in LinearLayout. 在您的代码中,您应该包含android“ windowMinWidthMajor ”和“ windowMinWidthMinor ”属性,并将dialog.getWindow()布局设置为MATCH_PARENT并在LinearLayout中进行布局。

<style name="Theme_Dialog_tab" parent="Theme.AppCompat.Dialog">
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowBackground">@null</item>
    </style>

In Java file: 在Java文件中:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.Theme_Dialog);

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

I hope it will help you....! 我希望它会帮助你......!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM