Snackbar

重要

本文說明 處於公開預覽 狀態的功能和指引,而且可能會在正式推出之前大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。

概觀

是具有CoordinatorLayout的自訂FrameLayout,做為子系,可用來做為列子系SnackbarContainer 此容器可折迭感知,而且可用於可折迭裝置,也可以用於一般裝置上。 每一次畫面底部的畫面底部會從螢幕邊緣固定 25 圖元顯示訊息。

使用 WindowManager 的資訊,此容器會移動開發人員需要的第一個畫面、第二個畫面或整個畫面上的 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 建立的,您必須將 kotlin-stdlib 相依性新增至模組層級 build.gradle 檔案 (這是因為使用 Kotlin) 建立的要庫。

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

如何顯示雙螢幕巴巴

新增套件之後,請遵循下列步驟來實作雙螢幕的[老式] 列:

  1. SnackbarContainer在 或 Fragment 根檢視底部 Activity 新增 :

    <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 實例,參數 snackbarContainermessage 是要顯示的文字,而且 LENGTH_LONG 是顯示持續時間。 函 show 式是擴充函式,用來在指定位置顯示 Snackbar 指定位置內的 SnackbarContainer

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

加巴列位置

參數的可能值為 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

第二個顯示區域會顯示一個[列] 列:

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

範例

您可以查看咖啡 條範例應用程式的 程式碼,以查看所有這些行為。