Android - ellipsize="end" 不显示三个点

Android - ellipsize="end" Not Showing Three Dots

我正在研究 TextView,它包含在 ConstraintLayout 中。

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

maxLines="1" 和 ellipsize="end" 在对我的问题进行彻底研究后似乎是最佳答案。不幸的是,它对我的​​情况不起作用。三个点根本没有显示。

这是快照:

原来的字符串是"Test for long description"(去掉了n)。它应该显示 "Test for long descrip...".

这是我的 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 文件中的一些重要内容?

Ellipsize 在 android 中非常痛苦,请在您的 textView 上应用以下内容:

txtDescription.setSingleLine(true);

希望对您有所帮助。

1) 在您的 Textview

中再添加一个 属性 android:singleLine="true"

(但已弃用,因此使用第二个选项)

2) 这三个一起使用

android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"

问题是:

android:maxLength="24"

删除它,因为您希望 TextView 被省略。
当 TextView 的宽度不足以显示整个文本时,它会变成椭圆形。
我想你不明白这个属性 android:ellipsize="end" 是什么意思。
如果它足够宽,那么您将不会在末尾看到任何点,因为不需要它们。
如果你设置 android:layout_width="40dp" 那么你会看到这些点。
对于 android:layout_width="wrap_content"TextView 足够宽并且不是椭圆形。

三点显示的关键是限制文本视图的宽度。 假设你贴图中的图片名称是left_image, 更改 TextView 的 xml 属性如下:

....
android:layout_width="0dp" 
app:layout_constraintStart_toEndOf="@+id/left_image"
....

以上两行限制了 TextView 的宽度(在左 img 和父级的右边框之间)。

在我的例子中,下面一行是问题所在。我把它注释掉了,它起作用了。

<item name="android:inputType">text|textNoSuggestions</item>

要解决此问题,您需要为文本视图使用固定宽度,而不是 xml 中的 maxLength 属性 在 xml 中:

android:maxLines="1"
android:ellipsize="end"

并且在 java/kotlin 程序中:[重要]

textDescription.setSelected(true) -> Java

textDescription.isSelected=true -> Kotlin