Can't get ellipsis to work on Android

2019-02-12 14:44发布

I have a TextView. I want it to ellipsize if longer than its available width. This does not work unless the input string has no spaces... can anyone provide an example of this working? I've tried different combinations of:

singleLine="true" maxLines="1" scrollHorizontally="false"

none of these have any effect. Again, if I supply a string that has no spaces in it, then the ellipsis appears correctly. What am I missing? I've tried this on 1.5, 1.6, 2.0, all same problem.

Thanks

4条回答
beautiful°
2楼-- · 2019-02-12 15:19

Simple Solution.

int limit = 9;
if (str.length() > limit) {
    textView.setText(str.substring(0, limit)+"...");
} else {
    textView.setText(str);
}
查看更多
Ridiculous、
3楼-- · 2019-02-12 15:23

Ellipsize is broken (go vote on the bug report, especially since they claim it's not reproducible) so you have to use a minor hack. Use:

android:inputType="text"
android:maxLines="1"

on anything you want to ellipsize. Also, don't use singleLine, it's been deprecated since 1.5.

查看更多
男人必须洒脱
4楼-- · 2019-02-12 15:23

this was the only combination that I could get to work on SDK ver4:

android:ellipsize="end"
android:singleLine="true"

(Yes I know it says it's deprecated but I'm left without a choice)

查看更多
forever°为你锁心
5楼-- · 2019-02-12 15:41

Try with:

textView.setSingleLine();
TruncateAt truncate = TruncateAt.END;
textView.setEllipsize(truncate);
查看更多
登录 后发表回答