How to get line count of textview before rendering?
问题 How can I get the number of lines a string will take up in a TextView before it is rendered. A ViewTreeObserver will not work because those are only fired after it is rendered. 回答1: final Rect bounds = new Rect(); final Paint paint = new Paint(); paint.setTextSize(currentTextSize); paint.getTextBounds(testString, 0, testString.length(), bounds); Now divide the width of text with the width of your TextView to get the total number of lines. final int numLines = (int) Math.ceil((float) bounds