4

我正在寻找一些关于如何正确使用它的示例代码TextView

我在 Google 搜索中发现的唯一东西是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);

但结果绝对不是它应该的样子。

更像是:萌、乔、艾萨克、贝莎……

而不是:Moe、Joe、Isaac + 3

4

1 回答 1

4
public static CharSequence commaEllipsize (CharSequence text, TextPaint p, 
                                       float avail, String oneMore, String more)

参数:

text - 截断的文本 p -
用来 测量 文本的
Paint当前语言环境

示例用法:

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);
于 2012-11-22T19:00:48.293 回答