You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Composable Row末尾元素超出视图宽度问题求助

解决Compose Row中文本过长导致末尾元素超出视图的问题

问题出在你的Row布局中,Text组件未设置宽度限制,当文本内容较长时会抢占剩余空间,导致右侧的IconButton被挤出屏幕。

修改方法很简单:给Text添加Modifier.weight(1f),让它占据Row中除Checkbox和IconButton之外的所有可用空间,同时保留maxLines和overflow属性处理文本溢出。另外原有的Spacer可以移除,因为weight已经能将IconButton推至右侧:

@Composable
fun ToDoCard(
    modifier: Modifier = Modifier,
    text: String = "",
    checked: Boolean = false,
    onCheckedChange: () -> Unit,
) {
    ElevatedCard(
        modifier = modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp, horizontal = 12.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.Start
        ) {
            Checkbox(
                checked = checked,
                onCheckedChange = { onCheckedChange() }
            )
            Text(
                text = text,
                fontSize = 20.sp,
                maxLines = 3,
                overflow = TextOverflow.Ellipsis,
                modifier = Modifier.weight(1f)
            )
            IconButton(
                onClick = { }
            ) {
                Icon(
                    imageVector = Icons.Default.Delete,
                    contentDescription = "Delete ToDo"
                )
            }
        }
    }
}

修改后,无论文本多长,Text都会在Checkbox和IconButton之间的空间内显示,超出部分以省略号截断,IconButton始终保持在屏幕可见区域内。

内容的提问来源于stack exchange,提问作者Antony Lajes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 18:32:44