Nascondi le barre del sistema per la modalità immersiva

L'esperienza di alcuni contenuti è ottimale a schermo intero senza alcun indicatore nella barra di stato o barra di navigazione. Alcuni esempi sono video, giochi, immagini gallerie, libri e slide di presentazioni. Definito come in modalità immersiva. Questa pagina mostra come coinvolgere più a fondo gli utenti con a schermo intero.

Figura 1. Esempio di modalità immersiva.

La modalità immersiva consente agli utenti di evitare uscite accidentali durante un gioco e offre un'esperienza immersiva per guardare immagini, video e libri. Tuttavia, fai attenzione alla frequenza con cui gli utenti accedono ed escono dalle app per controllare le notifiche. per effettuare ricerche estemporanee o eseguire altre azioni. Poiché la modalità immersiva fa sì che gli utenti non possano più accedere facilmente alla navigazione del sistema; usa solo la modalità immersiva quando il vantaggio dell'esperienza utente va oltre il semplice utilizzo di uno schermo aggiuntivo spazio.

Utilizza WindowInsetsControllerCompat.hide() per nascondere le barre di sistema e WindowInsetsControllerCompat.show() per ripristinarli.

Lo snippet seguente mostra un esempio di configurazione di un pulsante da 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 suo comportamento quando un utente interagisce con loro.

Specifica quali barre di sistema nascondere

Per specificare il tipo di barre di sistema da nascondere, trasmetti 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.