答案是使用 textView 的 Paint 对象的 breakText()。这是一个示例,
int totalCharstoFit= textView.getPaint().breakText(fullString, 0, fullString.length(),
true, textView.getWidth(), null);
现在totalCharstoFit 包含可以放入一行的确切字符。现在您可以创建一个完整字符串的子字符串并将其附加到 TextView 像这样,
String subString=fullString.substring(0,totalCharstoFit);
textView.append(substring);
并且要计算剩余的字符串,可以这样做,
fullString=fullString.substring(subString.length(),fullString.length());
现在是完整的代码,
在while循环中执行此操作,
while(fullstirng.length>0)
{
int totalCharstoFit= textView.getPaint().breakText(fullString, 0, fullString.length(),
true, textView.getWidth(), null);
String subString=fullString.substring(0,totalCharstoFit);
textView.append(substring);
fullString=fullString.substring(subString.length(),fullString.length());
}