英文:
Android jetpack compose ellipsis wrap content Text when it reaches its container
问题
在Jetpack Compose中实现相同的行为可能需要使用ConstraintLayout
来控制子元素的布局。以下是一个示例代码:
ConstraintLayout(
modifier = Modifier.fillMaxWidth()
) {
val (nameRef, countRef) = createGuidelineFromTopToBottom(0.5f)
Text(
modifier = Modifier
.constrainAs(nameRef) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(countRef)
}
.background(Color.Cyan),
text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
modifier = Modifier
.constrainAs(countRef) {
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
end.linkTo(parent.end)
}
.background(Color.Red),
text = "(0)"
)
}
请注意,这只是一个示例代码,实际的布局可能会根据您的需求和UI进行调整。同时,您可能需要调整createGuidelineFromTopToBottom
中的参数以获得所需的布局效果。
英文:
I'm currently working with android jetpack compose and i want to archive a behavior like this. I have UI like this . Here a TextView ("a name") is wrap_content
and when it reaches the end of the container it'll automatically be ellipsised like this .
So here in order to archive this behavior in XML I used
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:shrinkColumns="0"
app:layout_constraintEnd_toStartOf="@id/icon_container"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card">
<TableRow android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textAlignment="textStart"
tools:text="a name a name a name a name a name" />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="(213)" />
</TableRow>
</TableLayout>
I try to do the same behavior in jetpack compose but haven't found out a way to do it.
I try to do it with a Row but the first Text is not ellipsised until it push the second Text out of the Row and the width of itself is actually exceed the Row's width
Row(modifier = Modifier.fillMaxWidth()) {
Text(
modifier = Modifier
.background(Color.Cyan),
text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
modifier = Modifier
.background(Color.Red),
text = "(0)"
)
}
答案1
得分: 6
使用width(IntrinsicSize.Max)
替代fillMaxWidth()
在Row
上,并为第一个文本添加权重。
Row(modifier = Modifier.width(IntrinsicSize.Max)) {
Text(
modifier = Modifier.weight(1f)
.background(Color.Cyan),
text = "周末 周末 周末 周末 周末 周末 ",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
modifier = Modifier
.background(Color.Red),
text = "(0)"
)
}
Row
将尝试扩展其宽度以适应两个文本,直到达到最大宽度。在Row
内,第二个文本被放置,第一个文本占据其余的空间。
英文:
Use width(IntrinsicSize.Max)
instead of fillMaxWidth()
on Row and give weight to the first text.
Row(modifier = Modifier.width(IntrinsicSize.Max)) {
Text(
modifier = Modifier.weight(1f)
.background(Color.Cyan),
text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
modifier = Modifier
.background(Color.Red),
text = "(0)"
)
}
Row will try to expand its width to the size that can fit both texts until it reaches the max width.
Inside of Row, the second text is placed and the first take the rest of the space.
答案2
得分: 2
使用 Modifier.weight(),将 'fill' 参数设置为 false。
Row {
Text(
modifier = Modifier
.weight(1f, fill = false)
.background(Color.Cyan),
text = "周末 周末 周末 周末 周末 周末 ",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
modifier = Modifier.background(Color.Red),
text = "(0)"
)
}
英文:
Use Modifier.weight() with 'fill' parameter as false.
Row {
Text(
modifier = Modifier
.weight(1f, fill = false)
.background(Color.Cyan),
text = "Weekend Weekend Weekend Weekend Weekend Weekend ",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
modifier = Modifier.background(Color.Red),
text = "(0)"
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论