使文本完全适合屏幕尺寸 [英] Make text fit exactly as the screen size

查看:178
本文介绍了使文本完全适合屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有很长的文字,所以我已经决定把它分成页,而我把所以每次刷卡滚动到下一个页面。我做的方式是:

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++;
        }
    }

所以我把文成的页面,每个页面有500个字符。但由于Android这个心不是很好有不同的屏幕尺寸和形状,和换行符的arent计,因此可能会超出屏幕...
所以,任何人都可以提出一个办法知道需要多少个字符来填充屏幕,所以我可以使文字到另一个页面的其余部分?谢谢你。

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.

推荐答案

确定这里是一个镜头 - 和一个相当笨拙之一,但在很短:

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

所以

将一些文字适合在任何给定的行

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 ());
  } 

多少行贵的TextView有:

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)

请注意:的getHeight不能在构造函数中调用,因为它不知道的高度,但 - 你也可以做到这一点的onDraw或者onMeasure如果你有一个自定义的控制

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.

这篇关于使文本完全适合屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆