我想删除 TextField 的紫色线/指示符(见下图)。这是可能的还是我应该创建自己的自定义 TextField 来实现这一点?
4 回答
这已在最近的 Jetpack Compose UI Beta 版本1.0.0-beta01中进行了更改,现在您可以通过
具有所需颜色的TextFieldDefaults 。
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
backgroundColor = Color.LightGray,
)
例子
TextField(
value = searchText,
onValueChange = { Log.d(HOME_COMPONENT, it) },
label = { Text(text = "Search") },
shape = RoundedCornerShape(10.dp),
leadingIcon = {
Image(
painter = painterResource(id = R.drawable.ic_search),
contentDescription = "search"
)
},
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
backgroundColor = Color.LightGray,
)
)
或者如果您想根据您的 UI/UX 自定义组件,请使用BasicTextField
@Composable
fun ToolbarComponent() {
var searchText by remember { mutableStateOf("") }
Row(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically
) {
Icon(
painter = painterResource(id = R.drawable.ic_search),
contentDescription = "search",
modifier = Modifier.size(20.dp),
tint = iconTintColor
)
Spacer(modifier = Modifier.size(16.dp))
BasicTextField(
value = searchText,
onValueChange = { searchText = it },
modifier = Modifier
.background(shape = RoundedCornerShape(10.dp), color = Color.LightGray)
.fillMaxWidth()
.padding(16.dp),
decorationBox = {
Text(text = "Search")
}
)
}
}
首先,1.2.0-alpha04
您可以使用TextFieldDecorationBox
withBasicTextField
来构建基于 Material Design 文本字段的自定义文本字段。
在您的情况下,您可以应用indicatorLine
修饰符来定义focusedIndicatorLineThickness
和unfocusedIndicatorLineThickness
参数:
var text by remember { mutableStateOf("") }
val singleLine = true
val enabled = true
val interactionSource = remember { MutableInteractionSource() }
BasicTextField(
value = text,
onValueChange = { text = it },
modifier = Modifier
.indicatorLine(enabled, false,
interactionSource,
TextFieldDefaults.textFieldColors(),
focusedIndicatorLineThickness = 0.dp,
unfocusedIndicatorLineThickness = 0.dp
)
.background(
TextFieldDefaults.textFieldColors().backgroundColor(enabled).value,
TextFieldDefaults.TextFieldShape
)
.width(TextFieldDefaults.MinWidth),
singleLine = singleLine,
interactionSource = interactionSource
) { innerTextField ->
TextFieldDecorationBox(
value = text,
innerTextField = innerTextField,
enabled = enabled,
singleLine = singleLine,
visualTransformation = VisualTransformation.None,
interactionSource = interactionSource,
label = { Text("Label") }
)
}
否则,您可以使用TextField
定义这些属性:
focusedIndicatorColor
unfocusedIndicatorColor
disabledIndicatorColor
就像是:
TextField(
....
colors = TextFieldDefaults.textFieldColors(
backgroundColor = .....,
focusedIndicatorColor = Transparent,
unfocusedIndicatorColor = Transparent)
)
如果你使用TextField
in that 你可以activeColor
给Color.Transparent
注意:activeColor
不仅是指示器,它还用于标签底部指示器和光标
前任:
var text: String by mutableStateOf("")
TextField(value = text, onValueChange = {
text = it
}, activeColor = Color.Transparent)
根据文件,activeColor
是
activeColor 文本字段处于焦点时标签、底部指示器和光标的颜色
如果您想使用自己的,可以尝试BaseTextField
实际上(版本 alpha 7)这是我发现删除 Divider 的最简单的版本。
设置activeColor
和inactiveColor
toColor.Transparent
以隐藏 TextField 下的指示线,无论他处于何种状态。
如果仅添加inactiveColor
到Color.Transparent
,则仅当 TextField 未获得焦点时,该行才会不可见
添加textStyle = TextStyle(color = Color.White)
以显示颜色,无论何时 TextField 是否聚焦。
此解决方案将删除该行,但也会删除光标指示器。目前它不是最好的,但它实际上也是 Compose 上的 alpha 版本。
TextField(
value = MyValue,
onValueChange = { },
textStyle = TextStyle(color = Color.White),
activeColor = Color.Transparent,
inactiveColor = Color.Transparent,
shape = RoundedCornerShape(20)
)