Ẩn thanh hệ thống cho chế độ hiển thị tối đa

Tốt nhất là bạn có thể trải nghiệm một số nội dung ở chế độ toàn màn hình mà không có chỉ báo nào trên thanh trạng thái hoặc thanh điều hướng. Có thể kể đến một số ví dụ như video, trò chơi, thư viện ảnh, sách và trang trình bày. Chế độ này được gọi là chế độ chìm. Trang này cho biết cách bạn có thể thu hút người dùng sâu hơn bằng nội dung ở chế độ toàn màn hình.

Hình 1. Ví dụ về chế độ hiển thị tối đa.

Chế độ sống động giúp người dùng tránh vô tình thoát trong khi chơi, đồng thời mang đến trải nghiệm sống động khi thưởng thức hình ảnh, video và sách. Tuy nhiên, hãy lưu ý đến tần suất người dùng truy cập và rời khỏi ứng dụng để kiểm tra thông báo, thực hiện tìm kiếm ngẫu nhiên hoặc thực hiện các thao tác khác. Vì chế độ hiển thị tối đa khiến người dùng mất khả năng dễ dàng điều hướng hệ thống, nên chỉ sử dụng chế độ hiển thị tối đa khi lợi ích cho trải nghiệm người dùng không chỉ đơn thuần là sử dụng thêm không gian màn hình.

Hãy sử dụng WindowInsetsControllerCompat.hide() để ẩn thanh hệ thống và WindowInsetsControllerCompat.show() để hiển thị lại thanh hệ thống.

Đoạn mã sau đây cho thấy ví dụ về cách định cấu hình một nút để ẩn và hiện thanh hệ thống.

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

Nếu muốn, bạn có thể chỉ định loại thanh hệ thống để ẩn và xác định hành vi của các thanh hệ thống đó khi người dùng tương tác với chúng.

Chỉ định thanh hệ thống cần ẩn

Để chỉ định loại thanh hệ thống cần ẩn, hãy truyền một trong các tham số sau đến WindowInsetsControllerCompat.hide().

Chỉ định hoạt động của các thanh hệ thống bị ẩn

Sử dụng WindowInsetsControllerCompat.setSystemBarsBehavior() để chỉ định cách hoạt động của thanh hệ thống ẩn khi người dùng tương tác với các thanh đó.