پنهان کردن نوارهای سیستم برای حالت فراگیر

برخی‌از محتواها در حالت تمام‌صفحه بدون هیچ نشانگری در نوار وضعیت یا نوار پیمایش بهترین تجربه را ارائه می‌دهند. برخی‌از نمونه‌ها عبارت‌اند از ویدیو، بازی، گالری تصویر، کتاب، و اسلاید ارائه. به این حالت، حالت تمرکز بر محتوا می‌گویند. این صفحه نشان می‌دهد که چگونه می‌توانید با محتوای تمام‌صفحه، کاربران را عمیق‌تر درگیر کنید.

شکل ۱. نمونه‌ای از حالت فراگیر.

حالت فراگیر به کاربران کمک می‌کند از خروج تصادفی درطول بازی جلوگیری کنند و تجربه‌ای فراگیر برای لذت بردن از تصاویر، ویدیوها، و کتاب‌ها ارائه می‌دهد. بااین‌حال، به این نکته توجه داشته باشید که کاربران چند وقت یک‌بار برای بررسی اعلان‌ها، انجام جستجوهای فی‌البداهه، یا انجام اقدامات دیگر وارد برنامه‌ها می‌شوند و از آن‌ها خارج می‌شوند. چون حالت فراگیر باعث می‌شود کاربران دسترسی آسان به پیمایش سیستم را ازدست بدهند، فقط زمانی از حالت فراگیر استفاده کنید که مزیت آن برای تجربه کاربری فراتر از استفاده ساده از فضای اضافی صفحه‌نمایش باشد.

از WindowInsetsControllerCompat.hide() برای پنهان کردن نوارهای سیستم و از WindowInsetsControllerCompat.show() برای برگرداندن آن‌ها استفاده کنید.

تکه‌کد زیر نمونه‌ای از پیکربندی دکمه‌ای را نشان می‌دهد که نوارهای سیستم را پنهان و آشکار می‌کند.

کاتلین

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

درصورت تمایل، می‌توانید نوع نوارهای سیستم را برای پنهان کردن مشخص کنید و رفتار آن‌ها را هنگام تعامل کاربر با آن‌ها تعیین کنید.

مشخص کردن اینکه کدام نوار سیستم پنهان شود

برای مشخص کردن نوع نوار سیستم برای پنهان کردن، یکی از پارامترهای زیر را به WindowInsetsControllerCompat.hide() ارسال کنید.

مشخص کردن رفتار نوار سیستم پنهان

از WindowInsetsControllerCompat.setSystemBarsBehavior() برای مشخص کردن نحوه رفتار نوارهای سیستم پنهان هنگام تعامل کاربر با آن‌ها استفاده کنید.

  • از WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH برای آشکار کردن نوارهای سیستم پنهان در هر تعامل کاربر در نمایشگر مربوطه استفاده کنید.

  • از WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE برای آشکار کردن نوارهای سیستم پنهان در هر اشاره سیستم استفاده کنید، مثلاً تند کشیدن از لبه صفحه‌نمایش که نوار در آنجا پنهان شده است.

  • از WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE برای آشکار کردن موقت نوارهای سیستم پنهان با اشاره‌های سیستم استفاده کنید، مثلاً تند کشیدن از لبه صفحه‌نمایش که نوار در آنجا پنهان شده است. این نوارهای سیستم گذرا روی محتوای برنامه‌تان قرار می‌گیرند، ممکن است تاحدی شفاف باشند، و پس‌از مدت کوتاهی به‌طور خودکار پنهان می‌شوند.