Glance 互操作性

在某些情况下,您可能需要使用 XML 和 RemoteViews 来提供视图。 也许您已经实现了一项不使用 Glance 的功能,或者该功能尚不可用,或者无法通过当前的 Glance API 实现。对于这些情况,Glance 提供了 AndroidRemoteViews(一种互操作性 API)。

AndroidRemoteViews 可组合项允许将 RemoteViews 与其他可组合项放在一起:

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

像不使用 Glance 一样创建和定义 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,因为它用于放置定义的内容。