android - Jetpack Compose Android TextField 超出宽度并插入右侧元素变得狭窄

标签 android textfield word-wrap android-jetpack-compose

TextField(
    value = newCommentState.value,
    textStyle = TextStyle(
        fontSize = 16.sp,
        color = MaterialTheme.colors.primary
    ),
    onValueChange = { newCommentState.value = it },
    colors = TextFieldDefaults.textFieldColors(
        backgroundColor = Color.White
    ),
    keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
    placeholder = {
        Text(
            "Add your comment",
            color = colorResource(R.color.disabled_gray)
        )
    }
)
Spacer(modifier = Modifier.width(8.dp))
ClickableText(
    modifier = Modifier.defaultMinSize(70.dp),
    text = AnnotatedString("Post"),
    style = TextStyle(
        color = if (newCommentState.value.text.isNullOrEmpty())
            colorResource(
                R.color.disabled_gray
            ) else colorResource(R.color.green),
        fontSize = 20.sp,
    ),
    onClick = {
        if (newCommentState.value.text.isNullOrEmpty())
            Toast.makeText(
                context,
                "Cant post empty comment!",
                Toast.LENGTH_SHORT
            ).show()
        else
            coroutineScope.launch { createComment(context) }
    },
)  

我为一个文本字段编写了这段代码,该文本字段接受用户评论,并且旁边有一个可点击的文本“帖子”。当文本字段的文本超出宽度时,文本“Post”将被插入并换行。

enter image description here

我希望文本在不点击“发布”的情况下自动换行。

最佳答案

您可以通过将 Modifier.weight(1f) 添加到 TextField 来解决此问题。来自 documentation :

The parent will divide the horizontal space remaining after measuring unweighted child elements and distribute it according to this weight.

关于android - Jetpack Compose Android TextField 超出宽度并插入右侧元素变得狭窄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69605307/

相关文章:

ios - 如何在 SwiftUI TextField 组件中使用 textChange 事件?

SwiftUI TextField 在 ZStack 内的 VStack 中禁用(用 TextField 模拟警报)

android - Android 上的 libgdx 文本字段语言

python-3.x - 如何在 Jupyter 笔记本输出单元格中禁用换行?

html - 使用 Bootstrap 的表格单元格中的多行文本

android - 如何在 Android 电视盒上打开 Leanback 设置屏幕?

android - 自定义首选项,targetSdkVersion ="11": missing indent?

Android Aidl 错误 sdk 构建工具 29.0.0

android - 无法仅使用 APK 在 Android Studio 上运行 Robotium

visual-studio-2015 - 在 VS2015 Community Edition 中一行格式化属性