简体   繁体   English

与 Jetpack Compose 中的 VerticalScroll 结合使用时,fillMaxSize 修饰符不起作用

[英]fillMaxSize modifier not working when combined with VerticalScroll in Jetpack Compose

I tried looking this up but could not find anything relevant.我尝试查找此内容,但找不到任何相关内容。

I want to have a "full size" Column inside a vertically scrollable Box and this combination just doesn't seem to work.我想在一个可垂直滚动的Box有一个“全尺寸” Column ,而这种组合似乎不起作用。 when I add the verticalScroll(rememberScrollState()) modifier to the Box , it seems to "disable" the fillMaxSize() modifier of the Column当我将verticalScroll(rememberScrollState())修饰符添加到Box时,似乎“禁用”了ColumnfillMaxSize()修饰符

The following code does not work as expected:以下代码无法按预期工作:

MyTheme {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .border(2.dp, Color.Green) //for visual effect only
                .verticalScroll(rememberScrollState())
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(2.dp)
                    .border(2.dp, Color.Red) //for visual effect only
            ) {
               //some content
            }
        }
}

Expected result: Both Box and Column (green and red borders) fill the entire screen.预期结果: BoxColumn (绿色和红色边框)都填满了整个屏幕。

Actual result: Box fills the screen, but Column does not fill height实际结果: Box填满屏幕,但Column没有填满高度

However if I remove the verticalScroll() modifier from the Box , I get the expected result:但是,如果我从Box中删除verticalScroll()修饰符,我会得到预期的结果:

MyTheme {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .border(2.dp, Color.Green) //for visual effect only
                //verticalScroll modifier removed
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(2.dp)
                    .border(2.dp, Color.Red) //for visual effect only
            ) {
               //some content
            }
        }
}

verticalScroll wraps the height of the content, and can be stretched to very long, so the scope has Constraints.Infinity for maxHeight constraint. verticalScroll包裹了内容的高度,可以拉伸到很长,所以范围有Constraints.InfinitymaxHeight约束。

From fillMaxHeight documentation来自fillMaxHeight 文档

If the incoming maximum height is Constraints.Infinity this modifier will have no effect.如果传入的最大高度是 Constraints.Infinity,则此修饰符将无效。

That's why you need to set height explicitly.这就是为什么您需要明确设置height的原因。

Consider switching to LazyColumn (which has fillParentMaxHeight() for this exact purpose) or to Pager (which is made explicitly for such cases).考虑切换到LazyColumn (为此具有fillParentMaxHeight() )或Pager (明确用于这种情况)。

Also, as @AdrianK pointer out, with regular scrollable you can wrap your view with BoxWithConstraints , and use maxHeight to set height of your view.此外,作为@AdrianK 指针,您可以使用常规scrollable来包装您的视图BoxWithConstraints ,并使用maxHeight设置视图的height

BoxWithConstraints {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .border(2.dp, Color.Green)
            .verticalScroll(rememberScrollState())
    ) {
        Column {
            repeat(2) {
                Column(
                    modifier = Modifier
                        // fillMaxWidth instead of fillMaxSize
                        .fillMaxWidth()
                        // explicit height modifier
                        .height(this@BoxWithConstraints.maxHeight)
                        .padding(2.dp)
                        .border(2.dp, Color.Red)
                ) {
                    //some content
                }
            }
        }
    }
}

Result:结果:

You can wrap your layout into a BoxWithConstraints , which is not scrollable and grab the size from there:您可以将布局包装到BoxWithConstraints中,它不可滚动并从那里获取大小:

BoxWithConstraints {
    val pageSize = this.maxHeight // already provided as Dp value
    Box(
        modifier = Modifier
            .fillMaxSize()
            .border(2.dp, Color.Green) //for visual effect only
            .verticalScroll(rememberScrollState())
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .height(pageSize)
                .padding(2.dp)
                .border(2.dp, Color.Red) //for visual effect only
        ) {
            //some content
        }
        // just so we can scroll a bit further
        Spacer(modifier = Modifier.padding(top = pageSize).fillMaxWidth().height(72.dp))
    }
}

The solution with BoxWithConstraints pointed me to a working solution (in my case). BoxWithConstraints 的解决方案为我指出了一个可行的解决方案(就我而言)。

I wrap the Column in a max sized BoxWithConstraints .我将 Column 包装在最大尺寸的 BoxWithConstraints中。 Then I set the height of the Column to Modifier.然后我将 Column 的高度设置为 Modifier。 heightIn(min=maxHeight) .高度(最小=最大高度) This way the column is at least "maxHeight" tall.这样,该列的高度至少为“maxHeight”。

BoxWithConstraints(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Column(
            modifier = Modifier
                .heightIn(min = maxHeight)
                .verticalScroll(rememberScrollState())
        ) {
            Spacer(modifier = Modifier.weight(1f)
            Text(text="This is at the bottom")
        }
    }

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

相关问题 Jetpack Compose detectDragGestures 似乎仅在 Canvas 修改器设置为 box 内的 fillMaxSize 或 matchParentSize 时才有效 - Jetpack Compose detectDragGestures seems to be working only when Canvas modifier is set to fillMaxSize or matchParentSize inside a Box VerticalScroll 在 Jetpack 撰写列中不起作用 - VerticalScroll is not working in the Jetpack compose column Jetpack Compose:带有verticalScroll修饰符的列中的LazyVerticalGrid抛出java.lang.IllegalStateException - Jetpack Compose:LazyVerticalGrid within Column with verticalScroll modifier throws java.lang.IllegalStateException Jetpack Compose 中 Box 内组件中的 `fillMaxSize()` 和 `matchParentSize()` 之间有什么区别? - What is the difference between `fillMaxSize()` and `matchParentSize()` in a component inside a Box in Jetpack Compose? 使用可点击修饰符时 Jetpack compose 编译错误 - Jetpack compose compilation error when using clickable modifier 修改器在 Jetpack Compose 中不起作用 - The modifier does not work in Jetpack Compose Modifier.Scrollable 在 Android Jetpack Compose 中 - Modifier.Scrollable in Android Jetpack Compose onKeyEvent 修饰符在 Jetpack Compose 中不起作用 - onKeyEvent Modifier doesn't work in Jetpack Compose Jetpack Compose 从修改器中提取填充信息 - Jetpack Compose extract padding info from Modifier Jetpack compose 1.1.1 中不提供修改器重量 - Modifier weight not available in jetpack compose 1.1.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM