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))
}

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 である必要があります。