【问题标题】:Compose BasicTextField trailingIcon stretches the heigh of the fieldCompose BasicTextField trailing Icon 拉伸字段高度
【发布时间】:2022-12-05 23:15:04
【问题描述】:

我今天遇到了一个问题,如果我为 BasicTextField 指定尾随图标,它会强制增加字段的高度。有什么方法可以覆盖此设置? 正如您在图像中看到的那样,555g 字段比 Ingredient1 短,Ingredient1 由于添加了 trailingIcon 而变得更高。

我试图了解 compose 中发生了什么,在挖掘之后我在谷歌原生 TextField.kt 中遇到了这段代码

if (trailing != null) {
    Box(
        modifier = Modifier
            .layoutId(LeadingId)
            .then(IconDefaultSizeModifier),
        contentAlignment = Alignment.Center
    ) {
        trailing()
    }
}

所以我由此推断,IconDefaultSizeModifier 正在强制 minHeight 为 48.dp

下面是我用来创建 BasicTextField 的代码

BasicTextField(
    value = state,
    //... more arguments
    decorationBox = @Composable {
        TextFieldDefaults.OutlinedTextFieldDecorationBox(
            value = textVal,
            visualTransformation = VisualTransformation.None,
            innerTextField = it,
            singleLine = singleLine,
            enabled = enabled,
            interactionSource = interactionSource,
            trailingIcon = trailingIcon,
            // keep vertical paddings but change the horizontal
            contentPadding = TextFieldDefaults.textFieldWithoutLabelPadding(
                top = 10.dp,
                bottom = 10.dp,
                start = 12.dp,
                end = 8.dp
            ),
            container = {}
          )
    }
)

【问题讨论】:

    标签: android android-jetpack-compose android-compose-textfield


    【解决方案1】:

    您可以对两个 TextField 应用相同的高度。

    就像是:

     BasicTextField(
         modifier = Modifier.height(48.dp),
    

    【讨论】:

    • 我可以做到,但我想降低高度以适应更多线条....
    猜你喜欢
    • 1970-01-01
    • 2022-12-28
    • 2014-10-14
    • 1970-01-01
    • 2014-03-10
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多