简体   繁体   English

Jetpack Compose:随着文本的增长,TextField 在 AlertDialog 之外对齐

[英]Jetpack Compose: TextField aligns outside of AlertDialog as text grows

If you run the following composable and enter a long multiline text into the textfield, you will see that as the text grows, the textfield leaves the AlertDialog.如果您运行以下可组合并在文本字段中输入长的多行文本,您将看到随着文本的增长,文本字段会离开 AlertDialog。

Is there a way to fix this?有没有办法来解决这个问题?

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Preview
@Composable
fun MyComposable() {
    var text by remember {
        mutableStateOf("Press enter a couple of times to see the problem")
    }
    AlertDialog(
        onDismissRequest = { },
        title = {
            OutlinedTextField(
                value = text,
                onValueChange = { text = it },
                textStyle = MaterialTheme.typography.h5,
                label = { Text(text = "Text") },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(150.dp)
            )
        },
        confirmButton = {
            TextButton(
                onClick = {}
            ) {
                Text("Done")
            }
        },
        dismissButton = {
            TextButton(
                onClick = { }
            ) {
                Text("Cancel")
            }
        }
    )
}

Modifier.heightIn constrain the height of the content to be between mindp and maxdp as permitted by the incoming measurement Constraints. Modifier.heightIn 将内容的高度限制在输入测量约束所允许的 mindp 和 maxdp 之间。 If the incoming constraints are more restrictive the requested size will obey the incoming constraints and attempt to be as close as possible to the preferred size.如果传入的约束更具限制性,则请求的大小将服从传入的约束并尝试尽可能接近首选大小。

Column {
    var text by remember { mutableStateOf("some text")}
    OutlinedTextField(value = text,
        onValueChange = {text = it},
        textStyle = MaterialTheme.typography.headlineSmall,
        label = { Text(text = "Text") },
        modifier = Modifier.fillMaxWidth()
            .heightIn(min = 0.dp, max = 150.dp))
}

在此处输入图像描述

Textfield will grow to max height specified in heightIn modifier and then start scrolling.文本字段将增长到在 heightIn 修饰符中指定的最大高度,然后开始滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM