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”将被插入并换行。
我希望文本在不点击“发布”的情况下自动换行。
最佳答案
您可以通过将 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/