使文本完全适合屏幕尺寸

发布于 2024-11-08 00:34:06 字数 747 浏览 0 评论 0原文

我有一个很长的文本,所以我决定将其分成几页,然后我放了一个,这样每次滑动都会滚动到下一页。我这样做的方式是:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-11-15 00:34:06

好的,这是一个镜头 - 一个相当笨拙的镜头,但简而言之:

*您需要知道任何给定的文本行是否适合您的视图的宽度
*您需要知道您有多少条线路
*您需要处理嵌入的换行符

,以便

某些文本适合任何给定的行,

  private boolean isTooLarge (TextView text, String newText) {    
     float textWidth = text.getPaint().measureText(newText);
     return (textWidth >= text.getMeasuredWidth ());
  } 

您的文本视图有多少行:

numLinesPerPage=mTextView.getHeight()/mTextView.getLineHeight();  //not this doesn't handle special cases where you've changed the font, bolded it etc

使用这两个工具,您可以迭代您的文本,添加单词,跟踪您还需要处理多少行(以及如果文本包含换行符,请处理换行符)

注意: 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

  private boolean isTooLarge (TextView text, String newText) {    
     float textWidth = text.getPaint().measureText(newText);
     return (textWidth >= text.getMeasuredWidth ());
  } 

how many lines does your textview have:

numLinesPerPage=mTextView.getHeight()/mTextView.getLineHeight();  //not this doesn't handle special cases where you've changed the font, bolded it etc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文