android - 约束布局 : ellipsized text with "spring"?

标签 android android-layout android-constraintlayout

我尝试根据图片实现布局。问题是 TextView - 我需要同时换行短文本和省略长文本,但无法实现这一点:

  1. layout_width="wrap_content" - 换行可以,但椭圆形当然不行(因为使用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 并删除我用作“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/

相关文章:

android - ViewBinding - 不同风格的布局资源

android - 当文本超过一行时换行 TextView

android - 我如何以编程方式更改约束

android - 在 Constraint Layout 中对多个 View 进行分组以仅设置一个点击监听器

android - 我可以在 Android 1.5 中运行 Android 2.0 应用程序吗?

java - Android Studio 3.0 布局编辑器/预览器

android - 如何将android应用程序部署到设备上?

android - 在 Playstore 的新管理发布页面中更新推出旁边的百分比表示什么

android - 使用分隔线在 Android xml 中添加无边框按钮

android - 在 ConstraintLayout 中使用 ListView