Android TextView 中的分页 [英] Pagination in Android TextView
问题描述
我在文件 [~100 KB
] 中有一些文本需要在 TextView
中显示给用户.我想将文本分页.
I have some text in a file [~100 KB
] that needs to be displayed to the user in a TextView
. I want to split the text into pages.
这是我实现分页的想法:
This is the idea I have in mind to implement pagination:
- 确定屏幕宽度和屏幕高度,例如
320 x 480
. - 计算高度的 75%,[360
px
] 以容纳按钮等. - 确定字体大小
- 计算可以显示的字符数 [
N
]. - 从文件中读取并仅显示
N
个字符.
- Determine screen width and screen height, say
320 x 480
. - Calculate 75% of height, [360
px
] to accommodate buttons etc. - Determine font size
- Calculate number of characters [
N
] that can be displayed. - Read from file and display only
N
number of characters.
这看起来可行,但它看起来粗糙且容易出错.有人有更好的想法吗?
This seems like it would work, but it seems crude and error-prone. Does anyone have better ideas?
推荐答案
你应该创建你自己的 TextView 扩展并连接到 onMeasure 函数,它应该给你宽度和高度(可能想要给 textview layout_weight=1)
You should create your own extension of TextView and hook up into the onMeasure function which should give you the width and height (prob want to give the textview layout_weight=1)
然后你可以使用油漆来获得文本将占用的大小,不是我没有测试过这个,你可能需要做一些事情来解释换行符的分割.但这是一个好的开始...
Then you can use a paint to get the size that text will take up, not I've not tested this and you'll probably need to do something to account for splitting of newlines. But it is a good start...
Paint paint = new Paint();
Rect bounds = new Rect();
int text_height = 0;
int text_width = 0;
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(12);// have this the same as your text size
String text = "Some random text";
paint.getTextBounds(text, 0, text.length(), bounds);
text_check_h = bounds.height(); // Will give you height textview will occupy
text_check_w = bounds.width(); // Will give you width textview will occupy
这篇关于Android TextView 中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!