Jetpack Compose 是否像 XML 一样具有 maxHeight 和 maxWidth?

alf*_*tap 42 android android-jetpack-compose

我希望有一个最初包裹内容高度的 LazyColumn,但如果它达到一定高度(232dp),那么它会修复它的高度。在 XML 中,我会结合使用 maxHeight 和wrappContent,但我在 Compose 中找不到类似的东西?

Age*_*ntP 93

您可以像这样使用heightIn修饰符LazyColumn

LazyColumn(
    modifier = Modifier
        .heightIn(0.dp, 232.dp) //mention max height here 
        .widthIn(0.dp, 232.dp) //mention max width here 
) {                     
}
Run Code Online (Sandbox Code Playgroud)

这将确保如果添加更多项目,高度不会超过 232.dplazycolumn

  • 是否可以按分数设置 maxWidth? (7认同)
  • @JacobFerrero 使用 `BoxWithConstraints` /sf/answers/4749282541/ (4认同)
  • @JacobFerrero 在父布局中使用 onGloballyPositioned 获取最大宽度(以 px 为单位),将其转换为 dp,保存到 mutableStateOf 并将其传递给 widthIn 并使用一些乘数,例如 0.7f 表示最大 70 % 宽度 (3认同)