如何在1.2.0版本中移除TextField中的默认填充(includeFontPadding)?

10
compose_version = 1.2.0
kotlin 1.7.0

我试图移除TextField。因为我在使用它时需要一个尾随图标,所以我不能使用BasicTextField,因为它没有尾随图标。

我正在使用版本1.2.0,我认为includeFontPadding默认为false。但是,这并没有起作用。所以我试图显式地设置它如下:

textStyle = TextStyle(
                    platformStyle = PlatformTextStyle(
                        includeFontPadding = true
                    ))

然而,这种方法也行不通。所以我想知道版本1.2.0是否可行,并尝试去除默认填充。

    Column(modifier = Modifier
        .fillMaxWidth()
        .background(color = Color.White)
        .border(width = 2.dp, shape = RoundedCornerShape(4.dp), color = Color.LightGray)
        .height(56.dp)) {

        Text(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 16.dp),
            text = "Gender (optional)")

        TextField(
            textStyle = TextStyle(
                platformStyle = PlatformTextStyle(
                    includeFontPadding = true
                )),
            colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White),
            modifier = Modifier
                .fillMaxWidth(),
            value = rememberMobileCode,
            onValueChange = { newMobileCode ->
                rememberMobileCode = newMobileCode
            },
            trailingIcon = {
                Icon(dropdownIcon, contentDescription = "dropdown icon", modifier = Modifier.clickable {
                    rememberIsExpanded = !rememberIsExpanded
                })
            },
            singleLine = true,
            readOnly = true
        )
}
1个回答

8

从版本 1.2.0 开始,您可以使用 BasicTextField + TextFieldDecorationBox
您可以设置 trailingIcon,并且可以使用 contentPadding 属性来更改填充:

val colors = TextFieldDefaults.textFieldColors()

BasicTextField(
    value = text,
    onValueChange = { text = it },
    modifier = Modifier
        .fillMaxWidth()
        .background(
            color = colors.backgroundColor(enabled).value,
            shape = RoundedCornerShape(8.dp)
        ),
    interactionSource = interactionSource,
    enabled = enabled,
    singleLine = singleLine
) {
    TextFieldDefaults.TextFieldDecorationBox(
        value =text,
        innerTextField = it,
        singleLine = singleLine,
        enabled = enabled,
        visualTransformation = VisualTransformation.None,
        label = { Text(text = "label") },
        trailingIcon = {
            IconButton(onClick = {  }) {
                Icon(imageVector = Icons.Filled.Clear, contentDescription = "Clear")
            }
        },
        placeholder = { /* ... */ },
        interactionSource = interactionSource,
        // change the padding
        contentPadding = TextFieldDefaults.textFieldWithoutLabelPadding(
            top = 2.dp, bottom = 2.dp
        )
    )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接