ซ่อนแถบระบบสําหรับโหมดใหญ่พิเศษ

เนื้อหาบางอย่างจะให้ประสบการณ์การใช้งานที่ดีที่สุดเมื่อแสดงแบบเต็มหน้าจอโดยไม่มีตัวบ่งชี้ใดๆ บนแถบสถานะหรือแถบนำทาง เช่น วิดีโอ เกม แกลเลอรีรูปภาพ หนังสือ และสไลด์นำเสนอ ซึ่งเรียกว่า โหมดใหญ่พิเศษ หน้านี้จะแสดงวิธีดึงดูดให้ผู้ใช้มีส่วนร่วมกับเนื้อหาแบบเต็มหน้าจอมากขึ้น

รูปที่ 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 เพื่อแสดงแถบระบบที่ซ่อนอยู่ชั่วคราวเมื่อผู้ใช้ใช้ท่าทางสัมผัสของระบบ เช่น การปัดจากขอบหน้าจอที่ซ่อนแถบอยู่ แถบระบบชั่วคราวเหล่านี้จะซ้อนทับเนื้อหาของแอป อาจมีความโปร่งใสในระดับหนึ่ง และจะซ่อนโดยอัตโนมัติหลังจากหมดเวลาสั้นๆ