簡體   English   中英

如何更改 Android Jetpack Compose 中的 OutlineTextField 邊框寬度?

[英]How change OutlineTextField border width in Android Jetpack Compose?

我的代碼:

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),
)

我想增加邊框寬度,以便支持 colors focusedBorderColordisabledBorderColor

您可以像這樣更改 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
        )
    )

另一種解決方案是使用BaseTextField而不是OutlinedTextField

輪廓邊框在OutlinedTextField定義為常量值。

private val IndicatorUnfocusedWidth = 1.dp
private val IndicatorFocusedWidth = 2.dp

沒有直接的方法來覆蓋這些值。

因此,如果您需要實現動態邊框寬度,則必須創建完整的自定義 TextField Composable。

您可以復制粘貼OutlinedTextField.ktTextFieldImpl.kt的完整代碼,並根據需要修改它們以創建自定義可組合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM