Glance 互操作性

在某些情况下,您可能需要使用 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,因为它用于放置定义的内容。