ConstraintLayout:带有“ spring”的椭圆形文字?

问题描述

我尝试根据图片实现布局。问题是TextView-我需要同时包装短文本和ellipsize长文本,但是无法实现:

  1. layout_width="wrap_content"-自动换行是可以的,但是ellipsize当然不可以(因为当使用wrap_content时,因为没有施加约束)
  2. layout_width="0dp"-包装无效
  3. layout_width="0dp" + constraintWidth_default="wrap"-可以换行,不可以使用椭圆尺寸

也尝试过玩constrainedWidth="true",但是没有任何可见的效果

enter image description here

解决方法

哦,做到了!我曾使用解决方案来解决类似的问题(ConstraintLayout Chains and Text Ellipsis + Image on the Right)-直到我从链中排除了Button并删除了我用作“弹簧”的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>