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

问题描述 投票:0回答:4

我正在研究一种包含在TextView中的ConstraintLayout

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

在对我的问题进行彻底研究之后,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中有一些覆盖ellipsize的东西,或者我是否遗漏了XML文件中的关键内容?

android xml android-studio textview
4个回答
0
投票

问题是:

android:maxLength="24"

删除它,因为你想要TextView椭圆化。 当TextView不够宽以显示整个文本时,它会被椭圆化。 我想你不明白这个属性android:ellipsize="end"是什么。 如果它足够宽,那么你不会在最后看到任何点,因为它们不是必需的。 如果你设置android:layout_width="40dp"然后你会看到点。 使用android:layout_width="wrap_content"TextView足够宽,并且没有椭圆化。


0
投票

Ellipsize在android中非常痛苦,在你的textView下面应用:

txtDescription.setSingleLine(true);

希望能帮助到你。


0
投票

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

或者将这三个一起使用

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

0
投票

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

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

以上两行使TextView的宽度受到约束(在父级的左翼和右边界之间)。

© www.soinside.com 2019 - 2024. All rights reserved.