我的可组合项几乎符合我的喜好,最后一部分是在屏幕右上角添加一个图标。
问题来了,我使用
.weight(0.5f)
两次来确保我的数据具有自动最大长度,并且无论文本有多长,都以相同的宽度直观地显示。我怎样才能实现我的目标?
我的可组合代码:
Row(
modifier = Modifier
.fillMaxWidth()
.height(IntrinsicSize.Max)
)
{
Column{
// Other Stuff without ".weight" Modifier
}
Column(verticalArrangement = Arrangement.SpaceAround)
{
Row {
Row(
modifier = Modifier.weight(0.5f)
) {
Icon(
imageVector = Icons.Outlined.Person,
contentDescription = "desc"
)
Text(
text = "text 1",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
if (CustomConditionIsTrue) {
Row(
modifier = Modifier.weight(0.5f)
) {
Icon(
imageVector = Icons.Outlined.Home,
contentDescription = "desc"
)
Text(
text = "text 2",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
}
}
// Important Icon which need to be visible and accessible
Column(
modifier = Modifier.weight(0.2f),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.End
) {
Icon(
imageVector = Icons.Outlined.Info,
contentDescription = "important desc"
)
}
}
由于使用了
.weight(0.5f)
,我的Icons.Outlined.Info
不再显示(不可见)。.weight(0.5f)
后,我的 Icons.Outlined.Info
可见,但我的文本格式很差。
我在谷歌上搜索了有关在右上角绘制可组合项的信息,发现
Box
作为解决方案,不幸的是,它并没有“忽略”.weight
修饰符,结果是相同的(图标不可见)。
我已将
zIndex
值设置为 1.0E9f
,并希望无论 .weight
修改器如何,都会绘制图标,但这也不起作用(图标不可见)。如果确实如此,如果我考虑到这一点,它也可能会导致文本溢出......
我不确定我理解是否正确,但请尝试以下调整。
首先,将
weight
修饰符添加到包含嵌套行的列:
Column(
modifier = Modifier.weight(1f),
verticalArrangement = Arrangement.SpaceAround
) {
Row {
// the two nested Rows
}
}
然后,从信息图标中删除
weight
修饰符:
// Important Icon which need to be visible and accessible
Column(
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.End
) {
Icon(
imageVector = Icons.Outlined.Info,
contentDescription = "important desc"
)
}
输出: