在 Wear OS 裝置上,資訊方塊是由兩個主要元件算繪,且版本各自獨立。為確保應用程式的資訊方塊在所有裝置上都能正常運作,請務必瞭解這個基礎架構。
- Jetpack 資訊方塊相關程式庫:這些程式庫 (包括 Wear Tiles 和 Wear ProtoLayout) 會嵌入應用程式,開發人員可控管其版本。應用程式會使用這些程式庫建構
TileBuilder.Tile物件 (代表資訊方塊的資料結構),以回應系統的onTileRequest()呼叫。 - ProtoLayout 轉譯器:這個系統元件負責在螢幕上轉譯
Tile物件,並處理使用者互動。轉譯器版本不受應用程式開發人員控制,且可能因裝置而異,即使硬體相同也可能不同。
資訊方塊的外觀或行為可能會因應用程式的 Jetpack Tiles 程式庫版本,以及使用者裝置上的 ProtoLayout 轉譯器版本而異。舉例來說,某個裝置可能支援旋轉或顯示心率資料,而另一個裝置則不支援。
本文說明如何讓應用程式與不同版本的資訊方塊程式庫和 ProtoLayout 轉譯器相容。以及如何遷移至較高版本的 Jetpack 程式庫。
考量相容性
如要建立可在各種裝置上正常運作的資訊方塊,請考量不同裝置的功能支援情形。您可以透過兩種主要策略來達成這個目標:在執行階段偵測轉譯器功能,以及提供內建備用方案。
偵測轉譯器功能
您可以根據特定裝置上可用的功能,動態變更資訊方塊的版面配置。
偵測轉譯器版本
- 使用傳遞至
onTileRequest()方法的DeviceParameters物件的getRendererSchemaVersion()方法。這個方法會傳回裝置上 ProtoLayout 轉譯器的主要和次要版本號碼。 - 接著,您可以在
onTileRequest()實作中使用條件邏輯,根據偵測到的轉譯器版本調整資訊方塊的設計或行為。
@RequiresSchemaVersion 註解
- ProtoLayout 方法上的
@RequiresSchemaVersion註解,表示該方法要按照說明文件運作所需的最低轉譯器結構定義版本 (範例)。- 如果呼叫的方法需要比裝置上更高的轉譯器版本,雖然不會導致應用程式當機,但可能會導致內容無法顯示或功能遭到忽略。
版本偵測範例
val rendererVersion = requestParams.deviceConfiguration.rendererSchemaVersion val arcElement = // DashedArcLine has the annotation @RequiresSchemaVersion(major = 1, minor = 500) // and so is supported by renderer versions 1.500 and greater if ( rendererVersion.major > 1 || (rendererVersion.major == 1 && rendererVersion.minor >= 500) ) { // Use DashedArcLine if the renderer supports it … DashedArcLine.Builder() .setLength(degrees(270f)) .setThickness(8f) .setLinePattern( LayoutElementBuilders.DashedLinePattern.Builder() .setGapSize(8f) .setGapInterval(10f) .build() ) .build() } else { // … otherwise use ArcLine. ArcLine.Builder().setLength(degrees(270f)).setThickness(dp(8f)).build() }
提供備用方案
部分資源可讓您直接在建構工具中定義備用資源。這種做法通常比檢查轉譯器版本更簡單,因此建議優先採用。
常見用途是提供靜態圖片,做為 Lottie 動畫的備用圖片。如果裝置不支援 Lottie 動畫,則會改為算繪靜態圖片。
val lottieImage = ResourceBuilders.ImageResource.Builder() .setAndroidLottieResourceByResId( ResourceBuilders.AndroidLottieResourceByResId.Builder(R.raw.lottie) .setStartTrigger(createOnVisibleTrigger()) .build() ) // Fallback if lottie is not supported .setAndroidResourceByResId( ResourceBuilders.AndroidImageResourceByResId.Builder() .setResourceId(R.drawable.lottie_fallback) .build() ) .build()
使用不同的轉譯器版本進行測試
如要針對不同轉譯器版本測試資訊方塊,請將資訊方塊部署至不同版本的 Wear OS 模擬器。(在實體裝置上,ProtoLayout 轉譯器更新會透過 Play 商店或系統更新提供。無法強制安裝特定轉譯器版本。
Android Studio 的「資訊方塊預覽」功能會使用嵌入在 Jetpack ProtoLayout 程式庫中的轉譯器,而您的程式碼會依附於該程式庫,因此另一種方法是在測試資訊方塊時依附於不同的 Jetpack 程式庫版本。
遷移至 Tiles 1.5/ProtoLayout 1.3 (Material 3 Expressive)
更新 Jetpack 資訊方塊程式庫,即可享有最新強化功能,包括 UI 變更,讓資訊方塊與系統完美整合。
Jetpack Tiles 1.5 和 Jetpack ProtoLayout 1.3 推出多項重大改善和異動。包括:
- 用於描述 UI 的 Compose 類似 API。
- Material 3 Expressive 元件,包括底部邊緣按鈕,以及支援強化視覺效果:Lottie 動畫、更多漸層類型和新弧線樣式。 - 注意:即使不遷移至新版 API,也能使用部分功能。
建議
遷移資訊方塊時,請遵循下列建議:
- 同時遷移所有動態磚。請避免在應用程式中混用資訊方塊版本。雖然 Material 3 元件位於獨立構件 (
androidx.wear.protolayout:protolayout-material3) 中,技術上可在同一個應用程式中使用 M2.5 和 M3 資訊方塊,但除非絕對必要 (例如應用程式有大量資訊方塊,無法一次全部遷移),否則強烈建議不要採用這種做法。 - 採用資訊方塊使用者體驗指南。由於資訊方塊具有高度結構化和範本化的特性,因此請使用現有範例中的設計做為您自己設計的起點。
- 在各種螢幕和字型大小上進行測試。資訊方塊通常包含大量資訊,因此文字 (尤其是按鈕上的文字) 容易溢出和裁剪。為盡量減少這種情況,請使用預先建構的元件,並避免大量自訂。使用 Android Studio 的資訊方塊預先發布版功能,以及在多部實體裝置上進行測試。
遷移流程
如要遷移資訊方塊,請按照下列步驟操作:
更新依附元件
首先,請更新 build.gradle.kts 檔案。更新版本,並將 protolayout-material 依附元件變更為 protolayout-material3,如下所示:
// In build.gradle.kts
//val tilesVersion = "1.4.1"
//val protoLayoutVersion = "1.2.1"
// Use these versions for M3.
val tilesVersion = "1.6.2"
val protoLayoutVersion = "1.4.2"
dependencies {
// Use to implement support for wear tiles
implementation("androidx.wear.tiles:tiles:$tilesVersion")
// Use to utilize standard components and layouts in your tiles
implementation("androidx.wear.protolayout:protolayout:$protoLayoutVersion")
// Use to utilize components and layouts with Material Design in your tiles
// implementation("androidx.wear.protolayout:protolayout-material:$protoLayoutVersion")
implementation("androidx.wear.protolayout:protolayout-material3:$protoLayoutVersion")
// Use to include dynamic expressions in your tiles
implementation("androidx.wear.protolayout:protolayout-expression:$protoLayoutVersion")
// Use to preview wear tiles in your own app
debugImplementation("androidx.wear.tiles:tiles-renderer:$tilesVersion")
// Use to fetch tiles from a tile provider in your tests
testImplementation("androidx.wear.tiles:tiles-testing:$tilesVersion")
}
TileService 幾乎沒有變動
這項遷移作業的主要異動會影響 UI 元件。TileService 實作只需稍微修改,但 M3 ImageResource 元件 (icon()、avatarImage()、backgroundImage()) 需要 Material3TileService 或 materialScopeWithResources() 的 ProtoLayoutScope。
主要例外狀況涉及資訊方塊活動追蹤:如果應用程式使用 onTileEnterEvent() 或 onTileLeaveEvent(),建議您遷移至 onRecentInteractionEventsAsync()。從 API 36 開始,這些事件會批次處理。
調整版面配置生成程式碼
在 ProtoLayout 1.2 (M2.5) 中,onTileRequest() 方法會傳回 TileBuilders.Tile。這個物件包含多個元素,包括 TimelineBuilders.Timeline,其中包含描述資訊方塊 UI 的 LayoutElement。
在 ProtoLayout 1.3 (M3) 中,雖然整體資料結構和流程並未改變,但 LayoutElement 現在是採用受 Compose 啟發的方法建構,版面配置則根據定義的版位 (由上至下) titleSlot (選用;通常用於主要標題或標頭)、mainSlot (必填;用於核心內容) 和 bottomSlot (選用;通常用於邊緣按鈕等動作或短文字等補充資訊)。這個版面配置是由 primaryLayout() 函式建構而成。
比較 M2.5 和 M3 的版面配置函式
M2.5
fun myLayout( context: Context, deviceConfiguration: DeviceParametersBuilders.DeviceParameters ) = PrimaryLayout.Builder(deviceConfiguration) .setResponsiveContentInsetEnabled(true) .setContent( Text.Builder(context, "Hello World!") .setTypography(Typography.TYPOGRAPHY_BODY1) .build() ) .build()
M3
fun myLayout( context: Context, deviceConfiguration: DeviceParametersBuilders.DeviceParameters, ) = materialScope(context, deviceConfiguration) { primaryLayout(mainSlot = { text("Hello, World!".layoutString) }) }
以下列舉主要差異:
- 移除建構工具。Material UI 元件原先的建構工具模式,已由更具宣告性的 Compose 語法取代。(非 UI 元件,例如 String/Color/Modifiers,也會取得新的 Kotlin 包裝函式)。
- 標準化初始化和版面配置函式。M3 版面配置仰賴標準化的初始化和結構函式:
materialScope()和primaryLayout()。這些必要函式會初始化 M3 環境 (主題設定、使用materialScope的元件範圍),並定義主要版位版面配置 (使用primaryLayout)。每個版面配置都必須呼叫這兩個函式,且只能呼叫一次。
主題設定
Material 3 推出多項主題設定異動,包括動態色彩,以及更多字體排版和形狀選項。
顏色
Material 3 Expressive 的一項重要功能是「動態主題設定」:啟用這項功能 (預設為開啟) 的資訊方塊會以系統提供的主題顯示 (可用性取決於使用者的裝置和設定)。
M3 的另一項變更是擴充顏色符記數量,從 4 個增加到 29 個。您可以在 ColorScheme 類別中找到新的顏色符記。
字體排版
與 M2.5 類似,M3 高度依賴預先定義的字型大小常數,不建議直接指定字型大小。這些常數位於 Typography 類別中,提供稍微擴充的範圍,可提供更具表現力的選項。
如需完整詳細資料,請參閱字體排版說明文件。
圖案
大多數 M3 元件的形狀和顏色都可能有所不同。
textButton (在 mainSlot 中),形狀為 full:
形狀為 small 的相同 textButton:
元件
M3 元件比 M2.5 元件更具彈性,也更容易設定。M2.5 通常需要不同的元件來處理各種視覺效果,而 M3 則經常使用一般化、高度可設定的基本元件,並提供良好的預設值。
這個原則也適用於根層級版面配置。在 M2.5 中,這會是 PrimaryLayout 或 EdgeContentLayout。在 M3 中,建立單一頂層 MaterialScope 後,請呼叫 primaryLayout() 函式。這個函式會直接傳回根層級版面配置,不需要建構工具,並接受多個版位的 LayoutElements,例如 titleSlot、mainSlot 和 bottomSlot。您可以使用具體的 UI 元件 (例如 text()、button() 或 card() 傳回的元件) 或版面配置結構 (例如 LayoutElementBuilders 的 Row 或 Column) 填入這些版位。
主題是 M3 的另一項重要強化功能。根據預設,UI 元素會自動遵循 M3 樣式規格,並支援動態主題設定。
| M2.5 | M3 |
|---|---|
| 互動元素 | |
Button 或 Chip |
|
| 文字 | |
Text |
text() |
| 進度指標 | |
CircularProgressIndicator |
circularProgressIndicator() 或 segmentedCircularProgressIndicator() |
| 版面配置 | |
PrimaryLayout 或 EdgeContentLayout |
primaryLayout() |
| — | buttonGroup() |
| 圖片 | |
| — | icon()、avatarImage() 或 backgroundImage() |
修飾符
在 M3 中,用於裝飾或擴充元件的 Modifiers 更像 Compose。這項異動會自動建構適當的內部類型,減少樣板。(這項變更與 M3 UI 元件的使用無關;如有需要,您可以搭配使用 ProtoLayout 1.2 的建構工具樣式修飾符與 M3 UI 元件,反之亦然)。
M2.5
// Uses Builder-style modifier to set opacity fun myModifier(): ModifiersBuilders.Modifiers = ModifiersBuilders.Modifiers.Builder() .setOpacity(TypeBuilders.FloatProp.Builder(0.5F).build()) .build()
M3
// Uses Compose-like modifiers to set opacity fun myModifier(): LayoutModifier = LayoutModifier.opacity(0.5F)
您可以使用任一 API 樣式建構修飾符,也可以使用 toProtoLayoutModifiers() 擴充功能函式,將 LayoutModifier 轉換為 ModifiersBuilders.Modifier。
輔助函式
雖然 ProtoLayout 1.3 允許使用受 Compose 啟發的 API 來表示許多 UI 元件,但 LayoutElementBuilders 中的基礎版面配置元素 (例如列和欄) 仍會繼續使用建構工具模式。如要彌補這種風格差異,並提升與新 M3 元件 API 的一致性,建議您使用輔助函式。
不使用輔助函式
primaryLayout( mainSlot = { Column.Builder() .setWidth(expand()) .setHeight(expand()) .addContent(text("A".layoutString)) .addContent(text("B".layoutString)) .addContent(text("C".layoutString)) .build() } )
有幫手
// Function literal with receiver helper function fun column(builder: Column.Builder.() -> Unit) = Column.Builder().apply(builder).build() primaryLayout( mainSlot = { column { setWidth(expand()) setHeight(expand()) addContent(text("A".layoutString)) addContent(text("B".layoutString)) addContent(text("C".layoutString)) } } )
遷移至 Tiles 1.2/ProtoLayout 1.0
自 1.2 版起,大多數的資訊方塊版面配置 API 都位於 androidx.wear.protolayout 命名空間中。如要使用最新的 API,請完成下列程式碼遷移步驟。
更新依附元件
在應用程式模組的建構檔案中進行下列變更:
Groovy
// Removeimplementation 'androidx.wear.tiles:tiles-material:version'// Include additional dependencies implementation "androidx.wear.protolayout:protolayout:1.4.2" implementation "androidx.wear.protolayout:protolayout-material:1.4.2" implementation "androidx.wear.protolayout:protolayout-expression:1.4.2" // Update implementation "androidx.wear.tiles:tiles:1.6.2"
Kotlin
// Removeimplementation("androidx.wear.tiles:tiles-material:version")// Include additional dependencies implementation("androidx.wear.protolayout:protolayout:1.4.2") implementation("androidx.wear.protolayout:protolayout-material:1.4.2") implementation("androidx.wear.protolayout:protolayout-expression:1.4.2") // Update implementation("androidx.wear.tiles:tiles:1.6.2")
更新命名空間
在應用程式的 Kotlin 和 Java 程式碼檔案中進行下列更新: 或者,您也可以執行這段命名空間重新命名指令碼。
- 將所有的
androidx.wear.tiles.material.*匯入項目替換為androidx.wear.protolayout.material.*。請同時為androidx.wear.tiles.material.layouts程式庫完成這個步驟。 將大多數其他
androidx.wear.tiles.*匯入項目替換為androidx.wear.protolayout.*。androidx.wear.tiles.EventBuilders、androidx.wear.tiles.RequestBuilders、androidx.wear.tiles.TileBuilders和androidx.wear.tiles.TileService的匯入項目應維持不變。重新命名 TileService 和 TileBuilder 類別的幾個已淘汰的方法:
TileBuilders:將getTimeline()重新命名為getTileTimeline()、setTimeline()重新命名為setTileTimeline()TileService:將onResourcesRequest()重新命名為onTileResourcesRequest()RequestBuilders.TileRequest:將getDeviceParameters()重新命名為getDeviceConfiguration()、setDeviceParameters()重新命名為setDeviceConfiguration()、getState()重新命名為getCurrentState(),以及setState()重新命名為setCurrentState()