gpt4 book ai didi

android - Jetpack Compose 减少 TextField 的高度

转载 作者:行者123 更新时间:2023-12-04 14:57:24 29 4
gpt4 key购买 nike

我正在尝试设计一个像谷歌搜索栏这样高度降低的搜索栏。但是输入文本也被裁剪占位符文本。

            TextField(
value = searchText.value,
singleLine = true,
onValueChange = {
searchText.value = it
},
placeholder = { //placeholder text is also getting cropped

Text(
text = "Search",
fontSize = 20.sp,
)

},
textStyle = TextStyle(fontSize = 20.sp), // input text is getting cropped
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 10.dp)
.height(45.dp), // Here I have decreased the height
shape = RoundedCornerShape(22.dp),
)
我的输入文本和占位符文本被裁剪了 50%。
如何解决?
Search Field

最佳答案

我使用 TextField 遇到了同样的问题。显然,这是具有精确填充的 Material 组件,这会导致文本裁剪(即使字体较小)。
这是结果:
compose custom TextField
正如评论建议的解决方案是使用 BasicTextField,这里是代码:

@Composable
private fun CustomTextField(
modifier: Modifier = Modifier,
leadingIcon: (@Composable () -> Unit)? = null,
trailingIcon: (@Composable () -> Unit)? = null,
placeholderText: String = "Placeholder",
fontSize: TextUnit = MaterialTheme.typography.body2.fontSize
) {
var text by rememberSaveable { mutableStateOf("") }
BasicTextField(modifier = modifier
.background(
MaterialTheme.colors.surface,
MaterialTheme.shapes.small,
)
.fillMaxWidth(),
value = text,
onValueChange = {
text = it
},
singleLine = true,
cursorBrush = SolidColor(MaterialTheme.colors.primary),
textStyle = LocalTextStyle.current.copy(
color = MaterialTheme.colors.onSurface,
fontSize = fontSize
),
decorationBox = { innerTextField ->
Row(
modifier,
verticalAlignment = Alignment.CenterVertically
) {
if (leadingIcon != null) leadingIcon()
Box(Modifier.weight(1f)) {
if (text.isEmpty()) Text(
placeholderText,
style = LocalTextStyle.current.copy(
color = MaterialTheme.colors.onSurface.copy(alpha = 0.3f),
fontSize = fontSize
)
)
innerTextField()
}
if (trailingIcon != null) trailingIcon()
}
}
)
}
用改变的背景形状调用它:
CustomTextField(
leadingIcon = {
Icon(
Icons.Filled.Search,
null,
tint = LocalContentColor.current.copy(alpha = 0.3f)
)
},
trailingIcon = null,
modifier = Modifier
.background(
MaterialTheme.colors.surface,
RoundedCornerShape(percent = 50)
)
.padding(4.dp)
.height(20.dp),
fontSize = 10.sp,
placeholderText = "Search"
)

关于android - Jetpack Compose 减少 TextField 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67681416/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com