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