我现在不能尝试这个想法,但让我们试一试!根据
this post,您可以使用以下方法来确定字符串是否适合textview.
private boolean isTooLarge (TextView text, String newText) {
float textWidth = text.getPaint().measureText(newText);
return (textWidth >= text.getMeasuredWidth ());
}
因此,如果文本始终相同,您可以手动定义每个文本视图的初始值,然后在增加或减少重新计算的字体时,删除或添加单词.
如果不能手动输入初始值,您可以执行以下操作:
String wordsInTextview = new String();
int lastUsedWord= 0;
int totalNumberOfWords = text.size();
for (int i=lastUsedWord;i
if !(isTooLarge(TextView,wordsInTextview)) {
wordsInTextview = wordsInTextview + text(i); // add the words to be shown
} else {
TextView.setText(wordsInTextView);
lastUsedWord= i;
wordsInTextView = new String(); // no idea if this will erase the textView but shouldn't be hard to fix
}
}
您还需要存储textView的第一个单词的位置,因此当您调整文本大小时,您知道从哪里开始!
int firstWordOnTextView = TextView.getText().toString().split(" ")[0]
当它调整大小时,您可以使用相同的方法来计算屏幕上的文本.
lastUsedWord = firstWordOnTextView;
如果你想要更快,你可以跟踪你在每个页面上有多少单词,做一个平均值,并在几次运行后总是从那里统计你的循环.或者之前的几句话,以避免不得不迭代.
如果您不必一次显示太多页面,我相信这是一个合理的解决方案!
对不起代码中的错误我没有在哪里尝试它!有关此解决方案的任何评论非常有趣的问题!