הסתרת סרגלי המערכת למצב של צפייה היקפית

יש תכנים שמומלץ לצפות בהם במסך מלא בלי שיוצגו אינדיקטורים בסרגל הסטטוס או בסרגל הניווט. לדוגמה: סרטונים, משחקים, גלריות תמונות, ספרים ושקפים של מצגות. המצב הזה נקרא מצב אימרסיבי. בדף הזה מוסבר איך אפשר לעודד את המשתמשים להתעמק יותר בתוכן במסך מלא.

איור 1. דוגמה למצב אימרסיבי.

מצב עשיר מאפשר למשתמשים להימנע מיציאה מקרית במהלך משחק, ומספק חוויה עשירה לצפייה בתמונות ובסרטונים ולקריאת ספרים. עם זאת, חשוב לשים לב לתדירות שבה המשתמשים עוברים מאפליקציה אחת לאחרת כדי לבדוק התראות, לבצע חיפושים ספונטניים או לבצע פעולות אחרות. בגלל שבמצב עשיר המשתמשים לא יכולים לגשת בקלות לניווט במערכת, כדאי להשתמש במצב הזה רק אם היתרון לחוויית המשתמש גדול יותר משימוש בשטח מסך נוסף.

כדי להסתיר את סרגלי המערכת, משתמשים בקיצור הדרך WindowInsetsControllerCompat.hide(), וכדי להחזיר אותם, משתמשים בקיצור הדרך WindowInsetsControllerCompat.show().

בקטע הקוד הבא מוצגת דוגמה להגדרת לחצן להסתרה ולהצגה של סרגלי המערכת.

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

אפשר גם לציין את סוג סרגלי המערכת שרוצים להסתיר ולקבוע את ההתנהגות שלהם כשהמשתמש מקיים איתם אינטראקציה.

ציון סרגלי המערכת שרוצים להסתיר

כדי לציין את סוג סרגלי המערכת שרוצים להסתיר, מעבירים אחד מהפרמטרים הבאים אל WindowInsetsControllerCompat.hide().

הגדרת ההתנהגות של סרגלי מערכת מוסתרים

משתמשים ב-WindowInsetsControllerCompat.setSystemBarsBehavior() כדי לציין את ההתנהגות של סרגלי המערכת המוסתרים כשהמשתמש מקיים איתם אינטראקציה.

  • משתמשים ב-WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH כדי להציג את סרגלי המערכת המוסתרים בכל אינטראקציה של משתמשים במסך המתאים.

  • משתמשים ב-WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE כדי להציג את סרגלי המערכת המוסתרים בכל תנועות המערכת, כמו החלקה מהקצה של המסך שבו הסרגל מוסתר.

  • כדי להציג באופן זמני את סרגלי המערכת המוסתרים באמצעות תנועות במערכת, כמו החלקה מהקצה של המסך שבו הסרגל מוסתר, משתמשים ב-WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE. סרגלי המערכת האלה הם זמניים ומוצגים בשכבת-על מעל התוכן של האפליקציה. יכול להיות שהם שקופים במידה מסוימת, והם מוסתרים אוטומטית אחרי פסק זמן קצר.