[英]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
时,似乎“禁用”了Column
的fillMaxSize()
修饰符
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.预期结果: Box
和Column
(绿色和红色边框)都填满了整个屏幕。
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.Infinity
为maxHeight
约束。
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.