首页 > 解决方案 > Android - ellipsize="end" 不显示三个点

问题描述

我正在研究一个TextView包含在一个ConstraintLayout.

如果它的长度超过 maxLength,我希望 ellipsize 在文本的末尾(在 TextView 中)添加三个点。

在对我的问题进行彻底研究之后,maxLines="1" 和 ellipsize="end" 似乎是最好的答案。不幸的是,它不适用于我的情况。三个点根本没有出现。

这是快照:

在此处输入图像描述

原始字符串是“Test for long description”(删除了 n)。它应该显示“测试长描述......”。

这是我的 xml:

<TextView
    android:id="@+id/txtDescription"
    android:maxLength="24"
    android:maxLines="1"
    android:ellipsize="end"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="120dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    android:text="DescriptionView"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.72" />

我觉得 XML 中有一些覆盖椭圆大小的东西,还是我错过了 XML 文件中的一些重要内容?

标签: androidxmlandroid-studiotextview

解决方案


问题是:

android:maxLength="24"

删除它,因为您希望将TextView其椭圆化。
当 TextView 的宽度不足以显示整个文本时,它会变成椭圆形。
我认为您不了解此属性android:ellipsize="end"的全部含义。
如果它足够宽,那么最后你将看不到任何点,因为它们是不需要的。
如果您设置android:layout_width="40dp",那么您将看到这些点。 足够宽并且不是椭圆形的
android:layout_width="wrap_content"TextView


推荐阅读