在某些情況下,您可能想使用 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,做法與不使用 Glance 時相同,並將其做為參數傳遞。
此外,您也可以為可組合函式建立 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,因為它用於放置定義的內容。