Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

text - Pagination in Android 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:

  • 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...