Android-Kotlin:如何避免在显示对话框时屏幕向上移动?

Android-Kotlin: How to avoid having the screen shifting up when dialog is shown?

我实现了一个非常简单的对话框,如下图所示(这张图片取自 Android Studio 中的 Pixel 3a 模拟器):

它在大多数设备上运行良好,但在单个物理设备(LG G8 ThinQ)上,它让我在顶部发生奇怪的变化,如下面的屏幕截图所示:

这是对话框出现前的屏幕:

然后,这是对话框出现后的屏幕:

这是对话框关闭时的情况:

如您所见,屏幕仍然在顶部移动,超出了边界。我已尽一切努力避免这种情况,但无济于事。

调用对话框的函数如下:

 fun createNewFolderDialog() {
            val supportFragmentManager: FragmentManager = (activity as AppCompatActivity).supportFragmentManager
            CreateNewFolderDialogFragment().show(supportFragmentManager, "CreateNewFolderFragment")

        }

这是对话框 class:

class CreateNewFolderDialogFragment : DialogFragment() {



    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

       

        getDialog()!!.getWindow()?.setBackgroundDrawableResource(android.R.color.transparent);

        val dialog = inflater.inflate(R.layout.create_new_folder_dialog, container, false)

        fun submitNewFolderName() {

            val edit = dialog.findViewById(R.id.folder_name) as EditText
            val text = edit.text.toString()

            if (text != "") {
                getDialog()?.cancel()

                //Creates new folder...                

            }

        }

        //Set up listeners...
        val cnlButton: Button = dialog.findViewById(R.id.btn_cancel)
        cnlButton.setOnClickListener {
            
            getDialog()?.cancel()

           //Cancel...



        }

        val okButton: Button = dialog.findViewById(R.id.btn_ok)
        okButton.setOnClickListener {
            //Submit new folder...

        }

        return dialog
    }



    override fun onStart() {
        super.onStart()

        var width = 400
        width = width.toPx()
        
        dialog!!.window?.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
        dialog!!.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)       

        dialog!!.window?.decorView?.apply {
            // Hide both the navigation bar and the status bar.
            // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
            // a general rule, you should design your app to hide the status bar whenever you
            // hide the navigation bar.
            //systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

            systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    SYSTEM_UI_FLAG_HIDE_NAVIGATION

        }

    }



}

最后,这是对话框布局的 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parentFl"
    android:background="@android:color/transparent"
    android:paddingBottom="60dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_corner">




        <TextView
            android:id="@+id/new_folder_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="New Music Folder Name"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/new_folder_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:text="Enter the new name for the folder"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/new_folder_title" />

        <EditText
            android:id="@+id/folder_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:ems="10"
            android:hint="Folder name"
            android:inputType="text"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/new_folder_subtitle" />


        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="36dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/dialog_buttons_background"
            android:text="Cancel"
            android:textColor="@color/vsm_links"
            app:backgroundTint="@null"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btn_ok"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/folder_name" />



        <Button
            android:id="@+id/btn_ok"
            android:layout_width="0dp"
            android:layout_height="36dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/dialog_buttons_background"
            android:text="OK"
            android:textColor="@color/vsm_disabled"
            app:backgroundTint="@null"
            android:clickable="false"
            android:imeOptions="actionDone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/btn_cancel"
            app:layout_constraintTop_toBottomOf="@+id/folder_name" />




    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

关于如何解决这个问题的任何想法?

好的,感谢@cactustictacs 的提示。他是对的,问题与 LG 设备上的不同 API 有关。在所有模拟设备中,我有 API 30 (Android 11) 并且一切正常,而在 LG 上我有 API 28 (Android Pie) 并且我忽略了那个关键的区别。我用 API 28 和 29 创建了新的模拟设备,是的,我也可以在这些设备上重现同样的问题。

然后,我可以通过简单地观察每次对话框关闭时主 activity 中的 MutableLiveData 变量设置为“true”来解决问题,以便使用以下命令恢复全屏视图主代码 activity:

 binding.fullscreenContent.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LOW_PROFILE or
                        View.SYSTEM_UI_FLAG_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

瞧,问题现在已经解决了。只有在显示对话框时才会发生偏移,但一旦关闭,视图就会恢复为全屏。

再次感谢大家对我的帮助。