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

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

איור 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 כדי להציג באופן זמני סרגלי מידע מוסתרים באמצעות תנועות מובנות במערכת, כמו החלקה מקצה המסך שבו הסרגל מוסתר. סרגלי המידע הזמניים האלה מופיעים מעל התוכן של האפליקציה, יכול להיות שהם יהיו שקופים במידה מסוימת, והם מוסתרים באופן אוטומטי אחרי זמן קצר.