Alcuni contenuti possono essere visualizzati a schermo intero senza indicatori sulla barra di stato o di navigazione. come video, giochi, gallerie di immagini, libri e diapositive di presentazioni. Questa modalità è nota come modalità immersiva. Questa pagina mostra come coinvolgere gli utenti in modo più approfondito con i contenuti a schermo intero.
La modalità immersiva aiuta gli utenti a evitare le uscite accidentali durante un gioco e offre un'esperienza immersiva per la visione di immagini, video e libri. Tuttavia, fai attenzione alla frequenza con cui gli utenti entrano ed escono dalle app per controllare le notifiche, per eseguire ricerche estemporanee o per intraprendere altre azioni. Poiché la modalità immersiva fa sì che gli utenti perdano facilmente l'accesso alla navigazione del sistema, usa questa modalità solo se il vantaggio per l'esperienza utente va oltre il semplice utilizzo di spazio aggiuntivo sullo schermo.
Utilizza WindowInsetsControllerCompat.hide()
per nascondere le barre di sistema e WindowInsetsControllerCompat.show()
per ripristinarle.
Lo snippet seguente mostra un esempio di configurazione di un pulsante per nascondere e mostrare le barre di sistema.
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { ... val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView) // Configure the behavior of the hidden system bars. windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE // Add a listener to update the behavior of the toggle fullscreen button when // the system bars are hidden or revealed. ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, windowInsets -> // You can hide the caption bar even when the other system bars are visible. // To account for this, explicitly check the visibility of navigationBars() // and statusBars() rather than checking the visibility of systemBars(). if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) || windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) { binding.toggleFullscreenButton.setOnClickListener { // Hide both the status bar and the navigation bar. windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) } } else { binding.toggleFullscreenButton.setOnClickListener { // Show both the status bar and the navigation bar. windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) } } ViewCompat.onApplyWindowInsets(view, windowInsets) } }
Java
@Override protected void onCreate(Bundle savedInstanceState) { ... WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView()); // Configure the behavior of the hidden system bars. windowInsetsController.setSystemBarsBehavior( WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE ); // Add a listener to update the behavior of the toggle fullscreen button when // the system bars are hidden or revealed. ViewCompat.setOnApplyWindowInsetsListener( getWindow().getDecorView(), (view, windowInsets) -> { // You can hide the caption bar even when the other system bars are visible. // To account for this, explicitly check the visibility of navigationBars() // and statusBars() rather than checking the visibility of systemBars(). if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) || windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) { binding.toggleFullscreenButton.setOnClickListener(v -> { // Hide both the status bar and the navigation bar. windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()); }); } else { binding.toggleFullscreenButton.setOnClickListener(v -> { // Show both the status bar and the navigation bar. windowInsetsController.show(WindowInsetsCompat.Type.systemBars()); }); } return ViewCompat.onApplyWindowInsets(view, windowInsets); }); }
Facoltativamente, puoi specificare il tipo di barre di sistema da nascondere e determinare il loro comportamento quando un utente interagisce con esse.
Specifica le barre di sistema da nascondere
Per specificare il tipo di barre di sistema da nascondere, passa uno dei seguenti parametri a WindowInsetsControllerCompat.hide()
.
Utilizza
WindowInsetsCompat.Type.systemBars()
per nascondere entrambe le barre di sistema.Utilizza
WindowInsetsCompat.Type.statusBars()
per nascondere solo la barra di stato.Utilizza
WindowInsetsCompat.Type.navigationBars()
per nascondere solo la barra di navigazione.
Specifica il comportamento delle barre di sistema nascoste
Utilizza WindowInsetsControllerCompat.setSystemBarsBehavior()
per specificare il comportamento delle barre di sistema nascoste quando l'utente interagisce con esse.
Utilizza
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
per mostrare le barre di sistema nascoste in qualsiasi interazione dell'utente sul display corrispondente.Utilizza
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE
per visualizzare le barre di sistema nascoste su qualsiasi gesto di sistema, ad esempio scorrendo dal bordo dello schermo da cui è nascosta la barra.Utilizza
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
per mostrare temporaneamente le barre di sistema nascoste con i gesti di sistema, ad esempio scorrendo dal bordo dello schermo da cui è nascosta la barra. Queste barre di sistema temporanee si sovrappongono ai contenuti dell'app, potrebbero avere un certo grado di trasparenza e vengono nascoste automaticamente dopo un breve timeout.