gpt4 book ai didi

Android:Activity 中的 Jetpack Compose 和 XML

转载 作者:行者123 更新时间:2023-12-04 23:36:32 24 4
gpt4 key购买 nike

如何在同一个 Activity 中添加 Jetpack Compose 和 xml?一个例子将是完美的。

最佳答案

如果要在 XML 文件中使用 Compose,可以将其添加到布局文件中:

<androidx.compose.ui.platform.ComposeView
android:id="@+id/my_composable"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后,设置内容:
findViewById<ComposeView>(R.id.my_composable).setContent {
MaterialTheme {
Surface {
Text(text = "Hello!")
}
}
}
如果你想要相反,即在你的撰写中使用 XML 文件,你可以使用这个:
AndroidView(
viewBlock = { context: Context ->
val view = LayoutInflater.from(context)
.inflate(R.layout.my_layout, null, false)
val textView = view.findViewById<TextView>(R.id.text)
// do whatever you want...
view // return the view
},
update = { view ->
// Update the view
}
)

关于Android:Activity 中的 Jetpack Compose 和 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65648904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com