Systemleisten für immersiven Modus ausblenden

Einige Inhalte werden am besten im Vollbildmodus ohne Indikatoren in der Statusleiste oder Navigationsleiste angezeigt. Beispiele: Videos, Spiele, Bilder Galerien, Büchern und Präsentationsfolien. Dies wird als immersiven Modus. Auf dieser Seite erfahren Sie, wie Sie Nutzer mit Inhalte im Vollbildmodus anzuzeigen.

Abbildung 1: Beispiel für den immersiven Modus.

Im immersiven Modus können Nutzer das versehentliche Beenden eines Spiels vermeiden bietet ein immersives Erlebnis mit Bildern, Videos und Büchern. Berücksichtigen Sie jedoch, wie oft Nutzer Apps öffnen und wieder schließen, um Benachrichtigungen zu prüfen, spontane Suchanfragen durchzuführen oder andere Aktionen auszuführen. Da Nutzer im Vollbildmodus keinen einfachen Zugriff auf die Systemnavigation haben, sollten Sie ihn nur verwenden, wenn der Vorteil für die Nutzerfreundlichkeit über die zusätzliche Bildschirmfläche hinausgeht.

Mit WindowInsetsControllerCompat.hide() können Sie die Systemleisten ausblenden und mit WindowInsetsControllerCompat.show() wieder einblenden.

Das folgende Snippet zeigt ein Beispiel für die Konfiguration einer Schaltfläche zum Ein- und Ausblenden Systemleisten.

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)
    }
}
@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);
    });
}

Sie können optional den Typ der Systemleisten angeben, die ausgeblendet und bestimmt werden sollen. wenn Nutzende mit ihnen interagieren.

Angeben, welche Systembalken ausgeblendet werden sollen

Übergeben Sie einen der folgenden Parameter, um den Typ der Systembalken anzugeben, die ausgeblendet werden sollen an WindowInsetsControllerCompat.hide().

Verhalten ausgeblendeter Systemleisten festlegen

Mit WindowInsetsControllerCompat.setSystemBarsBehavior() können Sie festlegen, wie sich ausgeblendete Systemleisten verhalten, wenn Nutzer mit ihnen interagieren.