Android-ellipsize =“ end”不显示三个点

时间:2018-11-17 07:59:12

标签: android xml android-studio textview

我正在研究TextView中包含的ConstraintLayout

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

在对我的问题进行深入研究之后,

maxLines =“ 1”和ellipsize =“ end”似乎是最好的答案。不幸的是,它不适用于我的情况。三个点根本不显示。

这是快照:

enter image description here

原始字符串为“ 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中有一些覆盖ellipsize的东西,还是我错过了XML文件中的关键内容?

6 个答案:

答案 0 :(得分:0)

Ellipsize在android系统中是一种痛苦,请在下面的textView上应用:

txtDescription.setSingleLine(true);

希望有帮助。

答案 1 :(得分:0)

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

或将这三个一起使用

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

答案 2 :(得分:0)

问题是:

android:maxLength="24"

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

答案 3 :(得分:0)

显示三个点的关键是限制文本视图的宽度。 假设您发布的图中的图像名称为left_image, 更改TextView的xml属性,如下所示:

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

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

答案 4 :(得分:0)

在我的情况下,以下一行是问题所在。我将其注释掉,并且有效。

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

答案 5 :(得分:0)

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

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

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

textDescription.setSelected(true) -> Java

textDescription.isSelected=true -> Kotlin
相关问题