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

Một số nội dung sẽ có trải nghiệm tốt nhất ở chế độ toàn màn hình mà không cần bất kỳ chỉ báo nào trên thanh trạng thái hoặc thanh điều hướng. Một số ví dụ là video, trò chơi, thư viện hình ảnh, sách và trang trình bày. Đâ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 được tình trạng vô tình thoát trong khi chơi và mang lại 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 nhảy vào và rời khỏi ứng dụng để kiểm tra thông báo, tìm kiếm ngẫu nhiên hoặc thực hiện các hành động khác. Vì chế độ hiển thị tối đa khiến người dùng mất quyền truy cập dễ dàng vào thao tác trên hệ thống, nên bạn chỉ nên sử dụng chế độ hiển thị tối đa khi lợi ích về trải nghiệm người dùng không chỉ dừng lại ở việc sử dụng thêm không gian màn hình.

Sử dụng WindowInsetsControllerCompat.hide() để ẩn các thanh hệ thống và WindowInsetsControllerCompat.show() để đưa các thanh đó trở lại.

Đ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 các 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 đó khi người dùng tương tác với các thanh đó.

Chỉ định các 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 hành vi của thanh hệ thống ẩ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 chúng.

  • Sử dụng WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH để hiện các thanh hệ thống bị ẩn trong bất kỳ hoạt động tương tác nào của người dùng trên màn hình tương ứng.

  • Sử dụng WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE để hiện các thanh hệ thống bị ẩn trong mọi cử chỉ hệ thống, chẳng hạn như vuốt từ cạnh của màn hình nơi thanh bị ẩn.

  • Sử dụng WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE để tạm thời hiển thị các thanh hệ thống bị ẩn bằng các cử chỉ hệ thống, chẳng hạn như vuốt từ cạnh của màn hình nơi thanh bị ẩn. Các thanh hệ thống tạm thời này phủ lên nội dung ứng dụng, có thể có độ trong suốt nhất định và tự động bị ẩn sau một thời gian chờ ngắn.