相关疑难解决方法(0)

ConstraintLayout链和文本省略+右侧图像

2018年7月更新:

如果您正在使用ConstraintLayout 1.1.0,则使用正确的属性app:layout_constrainedWidth="true"代替旧的app:layout_constraintWidth_default="wrap"(和高度对应的).查看更新的答案.

2017年11月更新:

如果您使用的是ConstraintLayout 1.0.0稳定版(或以上版本)(此时为1.0.2版),请参阅更新的答案以获得更简单的解决方案,而无需嵌套布局.

原始问题:

使用ConstraintLayouts-Beta3于2016年11月3日发布.(来源)

我正在尝试执行以下操作:

ConstraintLayout 1.1.0

我用这样的布局实现了这个目标:

|                                        |
|<-[TextView]<->[ImageView] -----------> |
|                                        |
Run Code Online (Sandbox Code Playgroud)

这看起来不错,但是当文本比可用空间长时......

app:layout_constrainedWidth="true" 文本视图具有指定以下内容的样式:

  <TextView
      android:id="@+id/textView"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"

      app:layout_constraintHorizontal_chainStyle="packed"

      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintRight_toLeftOf="@+id/caret"
      app:layout_constraintHorizontal_bias="0.0"

      android:text="Some Text"
      android:textAlignment="viewStart"
      android:gravity="start" />

  <ImageView
      android:id="@+id/caret"
      android:layout_width="wrap_content"
      android:layout_height="8dp"
      app:layout_constraintDimensionRatio="1:1"

      app:layout_constraintLeft_toRightOf="@+id/textView"
      app:layout_constraintRight_toRightOf="parent"

      app:layout_constraintTop_toTopOf="@+id/textView"
      app:layout_constraintBottom_toBottomOf="@+id/textView"


      app:layout_constraintHorizontal_bias="0.0"

      android:contentDescription=""
      app:srcCompat="@drawable/ic_selection"
      android:layout_marginStart="8dp"/>
Run Code Online (Sandbox Code Playgroud)

所以它应该工作,但我不确定我需要什么约束让图像滑到右边然后在那里让文本视图理解没有更多的空间.

我错过了什么?

注意:如果我将textview的宽度设置为0dp,它可以工作,但是图像总是在右边(水平偏差似乎被忽略了)

注2:我也尝试过beta2,事实上,似乎Beta3 在可视化编辑器中有一个错误.

更新:我试图在Xcode/AutoLayout中复制它:

这是短文本的外观

短文

现在是相同的布局,我只需在文本视图中键入一个长文本...

长文

正如您可以看到图像视图的轨迹(右)约束所示:您从右边距开始有8个或更多 …

android android-studio android-constraintlayout constraint-layout-chains

31
推荐指数
1
解决办法
7074
查看次数