TextView的ellipsize属性在maxLines=1时不起作用

8

我真的不知道为什么,但我只能在maxLines=2及以上时才能让省略号生效。我展示了一些描述性的词语,然后是一个没有空格的长字符串。

这是TextView的样子:

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#757575"
                android:text="@string/gcm_not_registered"
                android:maxLines="1"
                android:ellipsize="end"
                android:id="@+id/login_gcmRegistrationTextView"/>

我使用编程方式设置文本,但根据maxLines的限制,我得到两个不同的结果:

maxLines=1 maxlines=2

唯一改变的是maxLines,为什么第一张图片中的行没有填满呢?


我在我的应用程序中也需要这个功能,而这个答案解决了我的问题。https://dev59.com/knI95IYBdhLWcg3wxA8-?answertab=votes#tab-top - Illegal Argument
你的字符串中有 \n 吗? - Illegal Argument
我不会。这是GCM ID,因此它只是纯文本。 - Martin Melka
3个回答

8

有两种方法可以解决它:

  1. 尝试将 android:ellipsize="end" 属性更改为 android:ellipsize="marquee"
  2. 尝试删除 android:maxLines="1" android:ellipsize="end" 属性,并添加 android:singleLine="true" 属性。

2)可行。但我想知道为什么,当我没有指定要省略文本时,它不应该被删节。我也看到这已经过时了,但我在整个文档中找不到任何关于它的文字。所以谢谢。 - Martin Melka

5

以下代码对我有用:

在xml中添加:

  • 属性ellipsizemarquee
  • 属性lines1

在Java中:

<yourTextView>.setHorizontallyScrolling(true);
<yourTextView>.setSelected(true);

如果有另一个项目请求“焦点”,您将失去跑马灯效果。为了防止这种情况,textView需要处于选定状态。


0
实际上问题出在可跨度文本上,如果您设置了可跨度文本,这将无法正常工作。除此之外,下面的代码对我有效。
<TextView
    android:id="@+id/tv_second"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40dp"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:fontFamily="sans-serif-condensed"
    android:textColor="#b3277b"
    android:background="#f7ecff"
    android:layout_below="@id/tv"
    android:text="This is first line\nThis is second line\nThis is third line"
    android:ellipsize="end"
    android:maxLines="2"
    />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接