Pagination in Android TextView

前端 未结 1 515
温柔的废话
温柔的废话 2020-12-15 01:34

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

1条回答
  •  抹茶落季
    2020-12-15 01:57

    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
    

    0 讨论(0)
提交回复
热议问题