我正在寻找一些示例代码,说明如何在TextView中正确地使用它。
我在谷歌搜索中唯一发现的就是这个TextUtils类的测试单元。
一些指导将是非常感谢。
编辑:
我查看了这里得到的答案,并试图在我的代码上实现它。我使用了以下代码片段:
TextView title = (TextView) view.findViewById(R.id.listitemThreadsTitle);
title.setVisibility(View.VISIBLE);
TextPaint p = title.getPaint();
String strTitle = "Moe, Joe, Isaac, Bethany, Cornelius, Charlie";
title.setText(strTitle);
float avail = p.measureText(strTitle);
CharSequence ch = TextUtils.commaEllipsize(strTitle, p, avail, "one more", "%d more");
title.setText(ch);但结果却完全不是我们应该做的。
更像是:莫伊,乔,艾萨克,贝塔.
代替:莫伊,乔,艾萨克+3
发布于 2012-11-22 19:00:48
public static CharSequence commaEllipsize (CharSequence text, TextPaint p,
float avail, String oneMore, String more)参数:
文本-要截断的文本
p -用来测量文本的油漆
the -文本可用的水平宽度
oneMore -当前区域设置中“多1”的字符串
more -当前区域设置中"%d多“的字符串
示例用法:
String text = "Apple, Orange, Mango, Banana";
TextView tv = new TextView(context);
float textWidth = tv.getPaint().measureText(text );
String tempStr = TextUtils.commaEllipsize(text, tv.getPaint(), textWidth,
"1 more", "%d more");
tv.setText(tempStr);更新:
TextView title = (TextView) view.findViewById(R.id.listitemThreadsTitle);
title.setVisibility(View.VISIBLE);
TextPaint p = title.getPaint();
String strTitle = "Moe, Joe, Isaac, Bethany, Cornelius, Charlie";
title.setText(strTitle);
float avail = title.getMeasuredWidth();
CharSequence ch = TextUtils.commaEllipsize(strTitle, p, avail, "one more", "%d more");
title.setText(ch);https://stackoverflow.com/questions/13518450
复制相似问题