資訊一覽互通性

在某些情況下,您可能需要使用 XML 和 RemoteViews 提供檢視畫面。您可能已在不使用 Glance 的情況下實作功能,或者目前的 Glance API 尚未提供或無法提供該功能。在這種情況下,Glance 會提供互通性 API AndroidRemoteViews。

AndroidRemoteViews 可組合函式可讓 RemoteViews 與其他可組合函式放在一起:

val packageName = LocalContext.current.packageName
Column(modifier = GlanceModifier.fillMaxSize()) {
    Text("Isn't that cool?")
    AndroidRemoteViews(RemoteViews(packageName, R.layout.example_layout))
}

建立並定義 RemoteViews,就像不使用資訊一覽一樣,並將其做為參數傳遞。

此外,您也可以為可組合項建立 RemoteViews 容器:

AndroidRemoteViews(
    remoteViews = RemoteViews(packageName, R.layout.my_container_view),
    containerViewId = R.id.example_view
) {
    Column(modifier = GlanceModifier.fillMaxSize()) {
        Text("My title")
        Text("Maybe a long content...")
    }
}

在本例中,系統會傳遞包含「容器」的版面配置,並定義 ID。這個容器必須是 ViewGroup,因為它是用來放置定義的內容。