スナックバー

重要

この記事では、パブリック プレビュー段階であり、一般公開前に大幅に変更される可能性がある機能とガイダンスについて説明します。 本書に記載された情報について、Microsoft は明示または黙示を問わずいかなる保証をするものでもありません。

概要

SnackbarContainer は、スナックバーのホルダーとして使用できる子として CoordinatorLayout を持つカスタム FrameLayout です。 このコンテナーは折りたたみ対応で、折りたたみ型デバイスだけでなく、通常のデバイスでも使用できます。 メッセージは、画面の端から固定された 25 ピクセルで、画面の下部に毎回表示されます。

WindowManager の情報を使用して、このコンテナーは、最初の画面、2 番目の画面、または画面全体で、開発者が必要とする CoordinatorLayout 子を移動します。 その他のシナリオでは、Snackbar を直接使用できます。

ライブラリをプロジェクトにインポートする方法

  1. mavenCentral() リポジトリが最上位の build.gradle ファイルにあることを確認します。

     allprojects {
         repositories {
             google()
             mavenCentral()
          }
     }
    
  2. モジュールレベルの build.gradle ファイルに依存関係を追加します。

    dependencies {
         implementation "com.microsoft.device.dualscreen:snackbar:1.0.0-alpha2"
    }
    

  1. Java を使用してプロジェクトを作成する場合は、モジュールレベルの build.gradle ファイルに kotlin-stdlib 依存関係を追加する必要があります。

    dependencies {
       implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    }
    

デュアルスクリーン スナックバーを表示する方法

パッケージが追加されたら、次の手順に従ってデュアルスクリーン スナックバーを実装します。

  1. Activity または Fragment ルート ビューの下部に SnackbarContainer を追加します。

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
    
        <com.microsoft.device.dualscreen.snackbar.SnackbarContainer 
            android:id="@+id/snackbar_container" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" /> 
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  2. SnackbarContainer のインスタンスを使用すると、このコード スニペットを使用して Snackbar を表示できます。 snackbarContainer パラメーターは SnackbarContainer のインスタンスであり、message パラメーターは表示されるテキストであり、LENGTH_LONG は表示期間です。 show 関数は、特定の位置に指定された SnackbarContainer 内に Snackbar を表示するために使用される拡張関数です。

    Snackbar
        .make(snackbarContainer.coordinatorLayout, message, LENGTH_LONG)
        .show(snackbarContainer, position) 
    

SnackbarPosition

position パラメーターに使用できる値は、次のとおりです。

  • SnackbarPosition.START
  • SnackbarPosition.END
  • SnackbarPosition.BOTH

以下、これらについて詳しく説明します。

SnackbarPosition.START

スナックバーは、最初の表示領域の下部に表示されます。

START: snackbar on the first screen, portrait orientation

START: snackbar on the first screen, landscape orientation

SnackbarPosition.END

スナックバーは、2 番目の表示領域に表示されます。

END: snackbar on the second screen, portrait orientation

END: snackbar on the second screen, landscape orientation

SnackbarPosition.BOTH

スナックバーは、表示領域全体の下部に表示されます。

BOTH: snackbar on both screens, portrait orientation

BOTH: snackbar on both screens, landscape orientation

サンプル

これらのすべての動作を確認するには、スナックバー サンプル アプリのコードを確認してください。