Jetpack Compose 中关于文本字段高度的查询

4
如何使文本框的高度随着内容增加而增加?我尝试使用类似以下代码的代码,但其高度是固定的,不会增加。

demo image

Scaffold(
    topBar = {

    },
    content = {
        Box(modifier = Modifier.padding(it)) {
            
            Column(){
                LazyColumn(modifier = Modifier.fillMaxHeight(0.9)){
                    // some composable here for UI
                }
            
                Row(){
                    // icon
                
                    BasicTextField(
                        maxLines = 4,
                    )
                
                    // icon
                }
            }
            
        }
    }
)

虽然我似乎通过给列0.9来固定高度,但我不知道如何在不给予固定高度的情况下使文本框高度随内容动态增长。
1个回答

6

检查您的约束和权重。

您可以使用类似以下的内容:

Column() {
    LazyColumn(modifier = Modifier.weight(1f)) {
       //....some composable here for UI
    }

    Row(){ /* .... */ }  
)

例如:
Row(
    Modifier.padding(horizontal = 8.dp),
    verticalAlignment = Alignment.CenterVertically
) {

    Icon(Icons.Filled.Add,"")

    BasicTextField(
        value = text,
        onValueChange = { text = it },
        modifier = Modifier
            .padding(2.dp)
            .weight(1f),
        maxLines = 4,
    ){ innerTextField ->
        Box(modifier = Modifier
            .background(LightGray,shape = RoundedCornerShape(4.dp))
            .padding(4.dp),
            contentAlignment = Alignment.CenterStart,){
            innerTextField()
        }
    }

    Icon(Icons.Filled.Send,"")
}

enter image description here


非常酷。那么,文本字段现在如何增加高度?这主要是因为将1f的权重添加到LazyColumn中吗? - mrtechmaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接