Nascondi le barre del sistema per la modalità immersiva

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.

Figura 1. Esempio di modalità immersiva.

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.
    window.decorView.setOnApplyWindowInsetsListener { 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())
            }
        }
        view.onApplyWindowInsets(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.
    getWindow().getDecorView().setOnApplyWindowInsetsListener((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 view.onApplyWindowInsets(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().

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.