DialogFragment 不占用无边框屏幕

DialogFragment does not occupy bezel less screen

我正在尝试在无边框 phone 中显示一个对话框片段,它有一个缺口。 这是截图。

如您所见,对话框片段并没有占据整个屏幕,而是在顶部显示出丑陋的灰色。

这是我试过的

我在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>

我对 Activity 屏幕使用了相同的技术,它可以工作,因为它占据了整个无边框屏幕,但这不适用于对话框片段

在您的代码中,您应该包含 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>

在 Java 文件中:

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

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

希望对你有所帮助....!

这里有两种选择。首先,让我们看看两个选项共有的组件;

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 的 onCreate 方法:

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

对话框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>

全屏对话框样式:

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

选项:

左侧(选项 #1)

将此行添加到 FullScreenDialogStyle

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

正确(选项 #2)

将此行添加到 FullScreenDialogStyle

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

我找到了另一个简单的解决方案,只需尝试将这段代码放在 onCreate() 方法上即可:

Dialog yourDialog;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    yourDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS );
    yourDialog.getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}