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