没入モードのシステムバーを非表示にする

一部のコンテンツは、ステータスバーやナビゲーション バーにインジケーターを表示せずに全画面表示で視聴するのが最適です。例として、動画、ゲーム、画像、 ギャラリー、書籍、プレゼンテーションのスライドがあります。これは「没入モード」と呼ばれます。このページでは、全画面表示を利用してユーザーに没入型のコンテンツを提供し、ユーザーのエンゲージメントを高める方法について説明します。

図 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 を使用する 非表示のシステムバーをシステム ジェスチャー( バーが非表示になっている画面の端からスワイプします。これらの一時的なシステムバーはアプリのコンテンツに重ねて表示されます。透明度を設定することもできます。また、短いタイムアウト後に自動的に非表示になります。