Android 如何创建一个不带背景或边框的文本字段?
我正在尝试创建一个文本字段,在Jetpack中使用一个底部,但没有任何其他边框或背景。我该怎么做 这是我当前使用的代码:Android 如何创建一个不带背景或边框的文本字段?,android,android-jetpack-compose,android-jetpack-compose-text,Android,Android Jetpack Compose,Android Jetpack Compose Text,我正在尝试创建一个文本字段,在Jetpack中使用一个底部,但没有任何其他边框或背景。我该怎么做 这是我当前使用的代码: val query = remember {mutableStateOf("")} TextField( value = query.value, onValueChange = { newValue -> query.value = newValue }, label=
val query = remember {mutableStateOf("")}
TextField(
value = query.value,
onValueChange = { newValue -> query.value = newValue },
label={Text("Dummy", color = colorResource(id = R.color.fade_green))},
textStyle = TextStyle(
textAlign = TextAlign.Start,
color = colorResource(id = R.color.fade_green),
fontFamily = FontFamily(Font(R.font.poppins_regular)),
fontSize = 14.sp,
),
modifier = Modifier
.padding(start = 30.dp).border(0.dp, Color.Red),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent
)
)
使用
1.0.0-beta04
,您只需使用:
var text by remember { mutableStateOf(TextFieldValue("")) }
TextField(
value = text,
onValueChange = {
text = it
},
label = { Text("label") },
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
//Color of indicator = underbar
focusedIndicatorColor = ....,
unfocusedIndicatorColor = ....,
disabledIndicatorColor = ....
)
)
如果要更改指示器宽度
,当前没有内置参数
您可以使用.drawback
修改器绘制一条线。比如:
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()
val indicatorColor = if (isFocused) Color.Red else Color.Gray
val indicatorWidth = 4.dp
TextField(
value = text,
onValueChange = {
text = it },
label={Text("Label")},
interactionSource = interactionSource,
modifier = Modifier
.drawBehind {
val strokeWidth = indicatorWidth.value * density
val y = size.height - strokeWidth / 2
drawLine(
indicatorColor,
Offset(0f, y),
Offset(size.width, y),
strokeWidth
)
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Transparent,
unfocusedIndicatorColor = Transparent,
disabledIndicatorColor = Transparent
)
)
非常感谢。我们如何改变车底的颜色?@SparshDutta只需使用
focusedIndicatorColor
,unfocusedIndicatorColor
,禁用indicatorcolor
。我已经更新了答案。有没有办法改变车底的宽度?比如变薄或变厚?@SparshDutta我已经更新了答案。当前没有indicatorWidth参数。