【发布时间】:2016-05-31 06:56:26
【问题描述】:
我想将省略号字符串从 ... 更改为自定义字符串,例如 abc...[more]。
但是在TextUtil中,省略号字符串是固定的:
private static final String ELLIPSIS_STRING = new String(ELLIPSIS_NORMAL);
【问题讨论】:
我想将省略号字符串从 ... 更改为自定义字符串,例如 abc...[more]。
但是在TextUtil中,省略号字符串是固定的:
private static final String ELLIPSIS_STRING = new String(ELLIPSIS_NORMAL);
【问题讨论】:
我很惊讶还没有任何好的解决方案,所以我花了一些时间编写了自己的自定义 TextView,它允许您显示自定义省略号。在开发它时我想到的几件事:
如果您发现任何问题,请查看并告诉我:https://github.com/TheCodeYard/EllipsizedTextView
这是一个截图:
【讨论】:
我在How to use custom ellipsis in Android TextView 找到了答案。但是必须在布局后运行该函数,可以在 OnGlobalLayoutListener.onGlobalLayout() 或 view.post() 上运行。
【讨论】:
试试这个方法
public void CustomEllipsize(final TextView tv, final int maxLine, final String ellipsizetext) {
if (tv.getTag() == null) {
tv.setTag(tv.getText());
}
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = tv.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if ((maxLine+1) <= 0) {
int lineEndIndex = tv.getLayout().getLineEnd(0);
String text = tv.getText().subSequence(0, lineEndIndex - ellipsizetext.length()) + " " + ellipsizetext;
tv.setText(text);
} else if (tv.getLineCount() >= (maxLine+1)) {
int lineEndIndex = tv.getLayout().getLineEnd((maxLine+1) - 1);
String text = tv.getText().subSequence(0, lineEndIndex - ellipsizetext.length()) + " " + ellipsizetext;
tv.setText(text);
}
}
});
}
并像这样使用这种方法
final TextView txtView = (TextView) findViewById(R.id.txt);
String dummyText = "sdgsdafdsfsdfgdsfgdfgdfgsfgfdgfdgfdgfdgfdgdfgsfdgsfdgfdsdfsdfsdf";
txtView.setText(dummyText);
CustomEllipsize(txtView, 2, "...[more]");
【讨论】: