安卓。计算适合屏幕的字符数

发布于 2024-12-20 05:09:07 字数 327 浏览 4 评论 0原文

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

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

发布评论

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

评论(3

孤檠 2024-12-27 05:09:07

您可能可以根据文本大小做出假设,也许这里的默认值是 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.

雅心素梦 2024-12-27 05:09:07

也许 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().

月牙弯弯 2024-12-27 05:09:07

也许你可以以某种方式计算适合文本视图的 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.

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