
DialogFragment隐藏导航栏
在 Android 中,使用 DialogFragment 显示对话框时,如果您希望隐藏系统导航栏(如状态栏和导航键),可以通过设置相关的系统 UI 标志来实现。这需要在 DialogFragment 的视图创建和显示过程中配置窗口属性。
以下是一个完整的例子,展示了如何在 DialogFragment 中隐藏系统导航栏(使用 Kotlin):
import android.os.Bundle
import android.view.View
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
class MyDialogFragment : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_my_dialog, container, false)
        return view
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.window?.decorView?.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }
}
在这个示例中:
DialogFragment 布局。确保你有一个对应的布局文件,例如 res/layout/fragment_my_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Your layout content goes here -->
</FrameLayout>DialogFragment要显示这个 DialogFragment,可以在活动或其他片段中调用以下代码:
val dialogFragment = MyDialogFragment()
dialogFragment.show(supportFragmentManager, "MyDialogFragmentTag")
通过在 DialogFragment 的 onViewCreated 方法中设置系统 UI 的可见性标志,可以实现隐藏系统导航栏的效果。这种方法允许您的 DialogFragment 在显示时全屏,并隐藏状态栏和导航栏。
Dialog中隐藏导航栏
在 Android 中,如果想在 Dialog 中隐藏系统导航栏(包括状态栏和底部的导航键),可以通过设置窗口属性来实现。在创建 Dialog 时,可以使用 Window 类提供的一些标志来隐藏导航栏。
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
class FullScreenDialog(context: Context) : Dialog(context) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE) // 去掉对话框的标题栏
        setContentView(R.layout.dialog_fullscreen)    // 设置对话框的布局
        
        window?.let {
            it.setLayout(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT
            )
            it.decorView.systemUiVisibility = 
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                View.SYSTEM_UI_FLAG_FULLSCREEN
        }
    }
}
在你的 Activity 中,通过以下方式显示自定义对话框:
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        val dialog = FullScreenDialog(this)
        dialog.show()
    }
}
确保你有一个对应的布局文件,例如 res/layout/dialog_fullscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Your layout content goes here -->
</FrameLayout>
1、 requestWindowFeature(Window.FEATURE_NO_TITLE):
2、 setContentView(R.layout.dialog_fullscreen):
3、 window?.let { … }:
window 不为 null,使用 Kotlin 的安全调用操作符 ?. 来使用窗口对象。通过这些设置,当显示 Dialog 时,它将隐藏系统导航栏和状态栏,实现全屏显示。如果需要在更多场景下多次使用该样式,可以进一步将其封装或抽取为一个通用组件。