Beberapa konten paling nyaman dinikmati dalam layar penuh tanpa indikator apa pun di status bar atau menu navigasi. Beberapa contohnya adalah video, {i>game<i}, galeri gambar, buku, dan {i>slide<i} presentasi. Mode ini disebut sebagai mode imersif. Halaman ini menunjukkan cara berinteraksi secara lebih mendalam dengan pengguna dengan konten dalam layar penuh.
Mode imersif membantu pengguna menghindari keluar yang tidak disengaja selama bermain game dan memberikan pengalaman yang imersif untuk menikmati gambar, video, dan buku. Namun, perhatikan seberapa sering pengguna masuk dan keluar aplikasi untuk memeriksa notifikasi, melakukan penelusuran secara mendadak, atau melakukan tindakan lain. Karena mode imersif menyebabkan pengguna kehilangan akses mudah ke navigasi sistem, gunakan mode imersif hanya jika manfaat pengalaman pengguna lebih dari sekadar menggunakan ruang layar tambahan.
Gunakan WindowInsetsControllerCompat.hide()
untuk menyembunyikan kolom sistem dan WindowInsetsControllerCompat.show()
untuk menampilkannya kembali.
Cuplikan berikut menunjukkan contoh konfigurasi tombol untuk menyembunyikan dan menampilkan kolom sistem.
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); }); }
Secara opsional, Anda dapat menentukan jenis kolom sistem yang akan disembunyikan dan menentukan perilakunya saat pengguna berinteraksi dengannya.
Menentukan kolom sistem yang akan disembunyikan
Untuk menentukan jenis kolom sistem yang akan disembunyikan, teruskan salah satu parameter berikut
ke WindowInsetsControllerCompat.hide()
.
Gunakan
WindowInsetsCompat.Type.systemBars()
untuk menyembunyikan kedua kolom sistem.Gunakan
WindowInsetsCompat.Type.statusBars()
untuk menyembunyikan status bar saja.Gunakan
WindowInsetsCompat.Type.navigationBars()
untuk hanya menyembunyikan menu navigasi.
Menentukan perilaku kolom sistem tersembunyi
Gunakan WindowInsetsControllerCompat.setSystemBarsBehavior()
untuk menentukan perilaku bar sistem tersembunyi saat pengguna berinteraksi dengannya.
Gunakan
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
untuk menampilkan kolom sistem tersembunyi pada interaksi pengguna apa pun pada tampilan yang sesuai.Gunakan
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE
untuk menampilkan kolom sistem tersembunyi pada gestur sistem apa pun, seperti menggeser dari tepi layar tempat panel disembunyikan.Gunakan
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
untuk sementara menampilkan kolom sistem tersembunyi dengan gestur sistem, seperti menggeser dari tepi layar tempat batang disembunyikan. Kolom sistem sementara ini menempatkan konten aplikasi Anda, mungkin memiliki tingkat transparansi tertentu, dan otomatis disembunyikan setelah waktu tunggu yang singkat.