[英]How change OutlineTextField border width in Android Jetpack Compose?
My code:我的代码:
OutlinedTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.fillMaxWidth().padding(start = 30.dp, end = 30.dp),
label = { Text(text = "Something", fontSize = 14.sp) },
shape = RoundedCornerShape(12.dp),
)
I want to increase the border width so that the colors focusedBorderColor
, disabledBorderColor
are supported.我想增加边框宽度,以便支持 colors
focusedBorderColor
和disabledBorderColor
。
You can change OutlinedTextField border like this您可以像这样更改 OutlinedTextField 边框
var hasFocus by remember { mutableStateOf(false) }
OutlinedTextField(
modifier = modifier
.border(
width = 1.dp,
color = if (hasFocus) Color.Red else Color.Unspecified
)
.onFocusChanged { focusState -> hasFocus = focusState.hasFocus },
colors = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = Color.Unspecified,
unfocusedBorderColor = Color.Unspecified
)
)
Another solution is to use BaseTextField
instead of OutlinedTextField
另一种解决方案是使用
BaseTextField
而不是OutlinedTextField
Outline border is defined as a constant value in OutlinedTextField
.轮廓边框在
OutlinedTextField
定义为常量值。
private val IndicatorUnfocusedWidth = 1.dp
private val IndicatorFocusedWidth = 2.dp
There is no direct way to override these values.没有直接的方法来覆盖这些值。
So, you have to create complete custom TextField Composables if you need to achieve dynamic border width.因此,如果您需要实现动态边框宽度,则必须创建完整的自定义 TextField Composable。
You can copy-paste the complete code in OutlinedTextField.kt
and TextFieldImpl.kt
and modify them as required to create the custom Composables.您可以复制粘贴
OutlinedTextField.kt
和TextFieldImpl.kt
的完整代码,并根据需要修改它们以创建自定义可组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.