有一个帮助我使用 windowManager 创建 popeUp,就像 trucaller 应用程序显示在其他带有 jetpack compose 的应用程序上?因为我搜索过,但我不喜欢谷歌中的任何例子
这是我的代码:
该代码不适用于我,它给了我错误
java.lang.IllegalStateException: LayoutNode should be attached to an owner
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun MyApp() {
val context = LocalContext.current
val window = context.getSystemService(WINDOW_SERVICE) as WindowManager
val layoutParams = WindowManager.LayoutParams().apply {
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
format = PixelFormat.TRANSLUCENT
flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
gravity = Gravity.CENTER
width = 400 // Set the desired width
height = 300 // Set the desired height
}
val overlayView = rememberOverlayView(window, layoutParams)
AndroidView(
factory = {
overlayView
},
modifier = Modifier.fillMaxSize()
){
}
}
@Composable
fun rememberOverlayView(window: WindowManager, layoutParams: WindowManager.LayoutParams): View {
val context = LocalContext.current
return remember {
val overlayView = object : FrameLayout(context) {
override fun onAttachedToWindow() {
super.onAttachedToWindow()
window.addView(this, layoutParams)
setContent {
ComposeOverlay()
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
window.removeView(this)
// Clean up resources if needed
}
}
if (overlayView.parent != null) {
(overlayView.parent as ViewGroup).removeView(overlayView)
}
window.addView(overlayView, layoutParams)
overlayView
}
}
@Composable
fun ComposeOverlay() {
Column(modifier = Modifier.padding(16.dp)) {
Text(text = "Hello from Compose!", color = Color.Black)
}
}
我问了chatGPt,但没有给我任何解决方案,对吧
要将 Jetpack compose 显示为其他应用程序顶部的浮动视图,您可以使用此库Floating-Bubble-View(顺便说一句,我是作者)。该库同时支持
XML
和 Jetpack Compose
,希望这会有所帮助。