【问题标题】:ConstraintLayout does not ellipsize long text in TextViewConstraintLayout 不会省略 TextView 中的长文本
【发布时间】:2018-04-26 05:40:13
【问题描述】:

我在图片右侧有一个TextView。我正在尝试在图像旁边放置一些长文本,但该文本应通过在末尾添加“...”来自动结束。但是,这不起作用。我使用这种布局:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp">

<ImageView
    android:id="@+id/file_icon"
    android:layout_width="250px"
    android:layout_height="250px"
    android:layout_gravity="center"
    android:clickable="true"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/file_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="This is a very long title and I hope I have the dots to break it"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/file_type"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintStart_toEndOf="@+id/file_icon"
    app:layout_constraintWidth_default="wrap"/>

<TextView
    android:id="@+id/file_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Type"
    android:textAppearance="@style/TextAppearance.AppCompat.Small"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/file_title"
    app:layout_constraintLeft_toRightOf="@+id/file_icon" />

<ImageView
    android:id="@+id/file_download"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:clickable="true"
    android:scaleType="centerInside"
    android:src="@drawable/ic_action_download"
    app:layout_constraintTop_toBottomOf="@+id/file_title"
    app:layout_constraintRight_toRightOf="parent" />

结果是这样的:

为什么长文本没有省略,以至于“...”出现在末尾?我已经阅读了thisthis 的帖子,但它对我不起作用。有人可以帮我吗?

【问题讨论】:

    标签: android android-layout textview android-constraintlayout


    【解决方案1】:

    使用 app:layout_constraintEnd_toEndOf="parent" 到您的 file_title TextView

    试试这个

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp">
    
        <ImageView
            android:id="@+id/file_icon"
            android:layout_width="250px"
            android:layout_height="250px"
            android:layout_gravity="center"
            android:clickable="true"
            android:scaleType="centerInside"
            android:src="@drawable/abc"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/file_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="This is a very long title and I hope I have the dots to break it"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            app:layout_constraintBottom_toTopOf="@+id/file_type"
            app:layout_constraintStart_toEndOf="@+id/file_icon"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed"
            app:layout_constraintEnd_toEndOf="parent"
    
            />
    
        <TextView
            android:id="@+id/file_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Type"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/file_icon"
            app:layout_constraintTop_toBottomOf="@+id/file_title" />
    
        <ImageView
            android:id="@+id/file_download"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:clickable="true"
            android:scaleType="centerInside"
            android:src="@drawable/abc"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/file_title" />
    
    </android.support.constraint.ConstraintLayout>
    

    输出

    【讨论】:

    • 太棒了!我尝试了很多次,但这终于成功了!谢谢!
    • 完美解决方案
    【解决方案2】:

    我经常遇到同样的问题,但原因不同。显然,当您使用 ellipsize 时,您必须将 android:layout_width="0dp" 用于 TextView。我经常错误地使用android:layout_width="wrap_content"。发布原始问题的人没有犯这个错误,但我指出这可能是“ConstraintLayout does not ellipsize long text in TextView”的另一个原因。

    【讨论】:

    • 这确实有效。这不是 TextView 及其 ellipsize 标签的要求,但正如ConstraintLayout documentation 指出的那样,如果您指定“0dp”,则视图将“匹配约束”。
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 2012-02-02
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多