android 可组合的drawLine在Column中看起来不同

cu6pst1q  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(150)

我正在学习编写和玩它。我的目的是创建自定义文本字段与提示动画以上的文本字段本身,但我不想使用标签回调。下面是我的组合使用列两次,但给我不同的底部边框线宽度。为什么呢?

@Composable
fun TextfieldCustom() {
    var text by remember { mutableStateOf(("")) }
    var hint by remember { mutableStateOf(("")) }
    var isClicked by remember { mutableStateOf((false)) }

    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(modifier = Modifier.height(20.dp).align(Alignment.Start)) {
            this@Column.AnimatedVisibility(
                visible = isClicked,
            ) {
                Text(text = "Hint")
            }
        }
        TextField(
            value = hint,
            onValueChange = {
                text = it
                hint = if (it.isNotEmpty()) {
                    text
                } else {
                    it
                }
            },
            colors = TextFieldDefaults.textFieldColors(
                textColor = Color.Gray,
                disabledTextColor = Color.Transparent,
                backgroundColor = Color.Transparent,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent,
                disabledIndicatorColor = Color.Transparent
            ),

            modifier = Modifier
                .onFocusChanged {
                    isClicked = it.isFocused
                }
                .background(Color.White)
                .drawBehind {
                    drawLine(
                        color = Color.Red,
                        start = Offset(0f, size.height),
                        end = Offset(size.width, size.height),
                        strokeWidth = 5.toDp().toPx()
                    )

                },
            placeholder = {
                if (!isClicked) {
                    Text(text = "Hint")
                } else {
                    Text(text = "")
                }
            },
        )
    }
}

结果-〉

活动中-〉

ComposeWrappedUpTheme {
                Column {
                    TextfieldCustom()
                    TextfieldCustom()
                }
            }

编辑:-〉我发现在setCotent块中添加fillMaxSize到Column可以解决这个问题。我不知道为什么。

lmvvr0a8

lmvvr0a81#

我知道答案,因为.drawBehind {}在可组合的边框后面绘制,所以在可组合的边框上使用1.dp的宽度和30的填充高度会使它看起来像31 dp。

相关问题