首页 > 解决方案 > 在 Kotlin 中显示对话框时如何隐藏片段中的底部导航栏?

问题描述

显示对话框时,我很难隐藏底部导航栏。我希望它不会出现,因为应用程序以全屏模式显示。

这是构建对话框的类:

class CreateNewFolderDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        return activity?.let {
            // Use the Builder class for convenient dialog construction
            //val builder = AlertDialog.Builder(it, R.style.AppBaseTheme)
            val builder = AlertDialog.Builder(it)
            val inflater = requireActivity().layoutInflater;

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.create_new_folder_dialog, null))
                    // Add action buttons
                    .setPositiveButton("OK",
                            DialogInterface.OnClickListener { dialog, id ->
                                // create folder ...
                            })
                    .setNegativeButton("Cancel",
                            DialogInterface.OnClickListener { dialog, id ->
                                getDialog()?.cancel()
                            })
            builder.create()


        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

这是它在另一个片段中的调用:

fun createNewFolderDialog() {

        val supportFragmentManager: FragmentManager = (activity as AppCompatActivity).supportFragmentManager

        val newFragment = CreateNewFolderDialogFragment()

        newFragment.show(supportFragmentManager, "newfolder")


}

这是我在显示对话框时得到的屏幕截图。如您所见,显示了底部导航栏。我想防止这种情况出现,永远。

出现对话框时显示底栏

标签: androidkotlindialogfragment

解决方案


您可以通过以下代码隐藏系统导航栏:

window.decorView.apply {
    // Hides the navigation bar.
    systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
}

隐藏系统导航栏的限制很少,您可以在下面的链接中查看。 源代码- Android 文档

您可以使用类的window对象,并可以在您的类中DialogFragment设置上述标志的方法,如下面的代码:CreateNewFolderDialogFragmentonStart

override fun onStart() {
    super.onStart()
    dialog?.window?.decorView?.apply {
        // Hide both the navigation bar
        systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    }
}

推荐阅读