使文本完全适合屏幕尺寸
我有一个很长的文本,所以我决定将其分成几页,然后我放了一个,这样每次滑动都会滚动到下一页。我这样做的方式是:
NumberOfPages=text.length()/CharPerPage; //CharPerPage=500;
NumberOfPages++;
Chapters.add(text.indexOf(" ", CurrentPage*CharPerPage));
CurrentPage++;
int tmp=0;
for(int i =NumberOfPages;i>0;i--)
{
tmp =(CurrentPage*CharPerPage);
if(tmp>text.length())
{
Chapters.add(text.length());
break;
}
else
{
Chapters.add(text.indexOf(" ", tmp));
CurrentPage++;
}
}
所以我将文本分成几页,每页有 500字符...但这并不好,因为Android具有不同的屏幕尺寸和形状,并且换行符不被计算在内,因此它可能超出屏幕... 那么有人可以建议一种方法来知道需要多少个字符来填充屏幕,以便我可以将其余的文本发送到另一个页面吗?谢谢。
I have a long text so I've decided to divide it into pages, and I put a so each swipe scrolls to the next page.. The way I did it was:
NumberOfPages=text.length()/CharPerPage; //CharPerPage=500;
NumberOfPages++;
Chapters.add(text.indexOf(" ", CurrentPage*CharPerPage));
CurrentPage++;
int tmp=0;
for(int i =NumberOfPages;i>0;i--)
{
tmp =(CurrentPage*CharPerPage);
if(tmp>text.length())
{
Chapters.add(text.length());
break;
}
else
{
Chapters.add(text.indexOf(" ", tmp));
CurrentPage++;
}
}
So I divide the text into pages, and each page has 500 chars... But this isnt good since Android has different screen sizes and shapes, and line breaks arent counted so it may go beyond the screen...
So can anyone suggest a way to know how many chars are needed to fill the screen so i can make the rest of the text to another page? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是一个镜头 - 一个相当笨拙的镜头,但简而言之:
*您需要知道任何给定的文本行是否适合您的视图的宽度
*您需要知道您有多少条线路
*您需要处理嵌入的换行符
,以便
某些文本适合任何给定的行,
您的文本视图有多少行:
使用这两个工具,您可以迭代您的文本,添加单词,跟踪您还需要处理多少行(以及如果文本包含换行符,请处理换行符)
注意: getHeight 不能在构造函数中调用,因为它还不知道高度 - 如果您有自定义控件,您也可以在 onDraw 或 onMeasure 中执行此操作。
Ok here is a shot - and a rather clumsy one but in short:
*You need need to know if any give line of text will fit width-wise in your view
*You need to know how many lines you have
*You need to handle embedded newlines
so
will some text fit on any given line
how many lines does your textview have:
With these two tools you could iterate through your text adding words keeping track of how many lines you have left to work with (and handle your newlines if your text contains them)
note: getHeight can't be called in constructor since it doesn't know the height yet - you could also do this in onDraw or maybe onMeasure if you have a custom control.