我有一个文本和一个图标可组合项。我希望图标粘在可组合项的右侧。这是我的代码:
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(
text = subjectName,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
textAlign = TextAlign.Center,
)
Icon(
painter = painterResource(id = R.drawable.ic_arrow_drop_down),
contentDescription = null
)
}
The corresponding UI is:
![](https://i.stack.imgur.com/RDh8Q.jpg)
This looks fine but when the text is too long and there is an overflow, the icon gets out of the screen like this:
![](https://i.stack.imgur.com/26PB3.jpg)
Instead I want to make it look like this:
![](https://i.stack.imgur.com/ZyGU1.jpg)
我尝试给予Text
可组合的weight(1f)
修改器,以便将图标放置在第一位。现在,溢出的文本看起来很好,但是当文本较短时,图标仍然放置在末尾,因为文本占据了整个剩余宽度:
我如何在这里获得所需的 UI(图 1 和 3)?
只需应用weight(1f, fill=false)
修饰符为Text
.
当填充为true
,该元素将被迫占据分配给它的整个宽度。否则,允许元素更小 - 这将导致 Row 更小,因为未使用的分配宽度不会重新分配给其他兄弟姐妹。
就像是:
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(
text = "subjectName",
modifier = Modifier.weight(1f, false),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Icon(
Icons.Filled.Add,
contentDescription = "contentDescription"
)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)