分页Android中的TextView分页、Android、TextView

2023-09-04 04:14:09 作者:十里春风不如她

我在一个文件中的一些文字[〜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×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