安卓。计算适合屏幕的字符数
我正在Android下写一个阅读器。页面被分成不同的视图。如何计算屏幕上适合多少个字符才能将文本正确划分到不同的View中?也许有一个简单的方法可以做到这一点? 向视图添加文本的代码:
for (int i = 0; i < pages; i++) {
TextView textView = new TextView(getApplicationContext());
textView.setText(text);
realViewSwitcher.addView(textView);
}
I am writing a reader under Android. Pages are separated into different View. How to calculate how many characters fit on the screen to correctly divide the text into different View? Maybe there is a simple method to do this?
The code for adding text to Views:
for (int i = 0; i < pages; i++) {
TextView textView = new TextView(getApplicationContext());
textView.setText(text);
realViewSwitcher.addView(textView);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能可以根据文本大小做出假设,也许这里的默认值是 12dip。然后假设平均单词长度为5个单词。确定 5 个字符的单词需要多少次倾斜。然后取宽度和高度并除以该数字。
you could probably make assumptions based on text size, maybe the default here would be 12dip. And then assuming that the average word length is 5 words. determining how many dips a 5 character words takes. and then taking the width and height and and divide it by that number.
也许 Paint 可以帮助你。有诸如“measureText”或“breakText”之类的方法。您可以使用 getPaint() 从 TextView 获取 Paint 对象。
Maybe Paint can help you. There is methods like "measureText", or "breakText". You can get a Paint object from a TextView with getPaint().
也许你可以以某种方式计算适合文本视图的 em 数量。
或者您可以创建一个算法来尝试基于computerVerticalScrollRange() 来适应字符。通过反复试验,它最终可以找到完美的配合。
perhaps you could calculate the number of ems that fit in the textview somehow.
or perhaps you could create an algorithm that try's to fit the characters based on computerVerticalScrollRange(). Through trial and error, it could eventually find the perfect fit.