简体   繁体   English

Jetpack Compose 可在另一个可组合物中使用更大尺寸的可组合物

[英]Jetpack Compose place composable with bigger size inside another composable

so I want to have a Box as a window with another box in it which is bigger than the outermost Box.所以我想有一个盒子作为一个窗口,里面有另一个比最外面的盒子大的盒子。 Then I want to have the inner Box draggable, so I can move it around inside the outermost box.然后我想让内框可以拖动,这样我就可以在最外面的框内移动它。

My Code looks like this:我的代码如下所示:

Box(modifier = Modifier
        .size(100f.dp, 100f.dp)
        .border(2f.dp, Color.Black, RectangleShape)
        .clipToBounds()
    ){

        val offsetX = remember { mutableStateOf(0f) }
        val offsetY = remember { mutableStateOf(0f) }

        Box(modifier = Modifier
            .offset(offsetX.value.dp, offsetY.value.dp)
            .size(600f.dp, 700f.dp)
            .background(Color.Blue) //DEBUG
            .pointerInput(Unit) {
                detectDragGestures { change, dragAmount ->
                    change.consumeAllChanges()
                    offsetX.value += dragAmount.x
                    offsetY.value += dragAmount.y
                }
            }
        ){
            //Contents of inner box
        }

    }

The problem is now, that the inner box is that the inner box is still only as big as the outer box.现在的问题是,内盒是内盒仍然只有外盒那么大。 So I can drag the inner box around but see that the background of the inner blue box is only as wide and high as the outer box.所以我可以拖动内框,但看到内蓝色框的背景仅与外框一样宽和高。

Is there another modifier to use, wo enable bigger composables inside other smaller composables?是否有其他修饰符可以使用,可以在其他较小的可组合物中启用更大的可组合物?

After a (long) bit of trying (I hate GUI...) here's my solution:经过(长时间)尝试(我讨厌 GUI ......)这是我的解决方案:

With .wrapContentSize(unbounded = true) set in the parent composable, the child composable can have a bigger size.在父可组合项中设置.wrapContentSize(unbounded = true)后,子可组合项可以具有更大的大小。

But make sure to set this modifier after .clipToBounds because otherwise the content won't clip as wished但请确保在.clipToBounds之后设置此修饰符,否则内容将无法按预期剪辑

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack Compose 不会在 setValue 上重新组合可组合 - Jetpack Compose not recomposing composable on setValue 在 Jetpack Compose 中按百分比偏移 Composable - Offset Composable by percentage in Jetpack Compose Jetpack Compose Column 可组合对齐 - Jetpack Compose Column Composable Alignment 使用 Jetpack Compose 仅​​在给定边界内拖动 Composable - Drag Composable only inside given boundries with Jetpack Compose 变量不使用jetpack compose在可组合内部更新 - Variable doesn't update inside composable using jetpack compose 如何在 Android Jetpack Compose 的可组合物中启动协程 - how to launch a coroutine inside a composable in Android Jetpack Compose 如何从另一个可组合屏幕为一个可组合屏幕异步安排 API 请求? (喷气背包组成) - How to schedule an API request asynchronously for one composable screen from another composable screen? (Jetpack Compose) 如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它 - How to pass a Composable to another Composable as its parameter and display/run it in Jetpack Compose Jetpack Compose:是否可以在单击另一个可组合项 function 中的按钮时调用可组合项 function? - Jetpack Compose: Is it possible to call an Composable function on a button click in another Composable function? 使用 Jetpack Compose 的深层链接导航到可组合项 - Navigating to a composable using a deeplink with Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM