Ukryj paski systemowe w trybie pojemnym

Niektóre treści najlepiej oglądać w trybie pełnoekranowym bez żadnych wskaźników na pasku stanu ani na pasku nawigacyjnym. Są to na przykład filmy, gry, galerie obrazów, książki i slajdy. Jest to tzw. tryb pojemny. Ta strona pokazuje, jak możesz bardziej zaangażować użytkowników, wyświetlając treści na pełnym ekranie.

Rysunek 1. Przykład trybu pojemnego.

Tryb pojemny pomaga użytkownikom uniknąć przypadkowego zamknięcia gry i zapewnia imponujące wrażenia podczas oglądania obrazów, filmów i książek. Zwróć jednak uwagę na to, jak często użytkownicy uruchamiają i wychodzą z aplikacji, aby sprawdzić powiadomienia, przeprowadzić niezaplanowane wyszukiwania lub wykonać inne działania. Tryb pojemny powoduje, że użytkownicy tracą łatwy dostęp do nawigacji w systemie, dlatego używaj go tylko wtedy, gdy korzyści dla użytkownika są większe niż tylko wykorzystanie dodatkowego miejsca na ekranie.

Użyj narzędzia WindowInsetsControllerCompat.hide(), aby ukryć paski systemowe, i WindowInsetsControllerCompat.show(), aby je przywrócić.

Fragment kodu poniżej pokazuje przykład konfigurowania przycisku do ukrywania i wyświetlania słupków systemowych.

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

Opcjonalnie możesz określić typ pasków systemowych, które mają być ukryte, i określić ich działanie, gdy użytkownik wejdzie z nimi w interakcję.

Określ, które paski systemowe mają być ukryte

Aby określić typ słupków systemowych do ukrycia, przekaż jeden z tych parametrów do funkcji WindowInsetsControllerCompat.hide().

Określ zachowanie ukrytych pasków systemowych

Użyj właściwości WindowInsetsControllerCompat.setSystemBarsBehavior(), aby określić, jak działają ukryte paski systemowe, gdy użytkownik wejdzie z nimi w interakcję.