我尝试根据图片实现布局。问题是 TextView - 我需要同时换行短文本和省略长文本,但无法实现这一点:
layout_width="wrap_content"
- 换行可以,但椭圆形当然不行(因为使用wrap_content时没有应用任何约束)layout_width="0dp"
- 换行不起作用layout_width="0dp"
+constraintWidth_default="wrap"
- 换行可以,椭圆不行
还尝试使用constrainedWidth="true"
,但没有任何明显的效果。
最佳答案
哦,做到了!我使用了类似问题的解决方案( ConstraintLayout Chains and Text Ellipsis + Image on the Right ) - 它不会有帮助,直到我从链中排除 Button
并删除我用作“spring”的Space
“!
这是结果 - 与图片中的完全一样。
<ConstraintLayout
android:layout_width="match_parent"
...>
<TextView
android:id="@+id/text"
android:layout_width="0dp" // enable constraints
android:ellipsize="end"
android:lines="1"
app:layout_constraintHorizontal_chainStyle="packed" // keep text + icon side by side
app:layout_constraintHorizontal_bias="0" // move text + icon to the left
app:layout_constraintWidth_default="wrap" // make text as small to wrap text
app:layout_constraintLeft_toLeftOf="parent" // chain starts
app:layout_constraintRight_toLeftOf="@id/icon" // chain continues
.../>
<ImageView
android:id="@+id/icon"
app:layout_constraintLeft_toRightOf="@id/text" // chain continues
app:layout_constraintRight_toLeftOf="@id/button" // chain ENDS!
.../>
<Button
android:id="@+id/button"
app:layout_constraintRight_toRightOf="parent" // move button to the right
// PAY ATTENTION! There is no constraintLeft_toRightOf="icon" attribute!
// because button must be out of chain
.../>
</ConstraintLayout>
关于android - 约束布局 : ellipsized text with "spring"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63572521/