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

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

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