Method 1
Although a method used in work is not particularly accurate, the effect is still good. Let me share it here.
/** * Get the maximum number of words to display in textview * @param text text content * @param size Text font size * @param maxWidth maximum width of textview * @return */ private float getLineMaxNumber(String text, float size,float maxWidth) { if (null == text || "".equals(text)){ return 0; } Paint paint = new Paint(); (size); //Get the overall length of the text content float textWidth = (text); // textWidth float width = textWidth / (); float total = maxWidth / width; return total; }
The above method is not very accurate, but it is more suitable for use in RecyclerView or ListView to avoid generating too many objects
Method 2
/** * Get the maximum number of words that can be displayed in a line of textview (need to be after the TextView measurement is completed) * * @param text text content * @param paint () * @param maxWidth ()/or is the specified value, such as 200dp */ private int getLineMaxNumber(String text, TextPaint paint, int maxWidth) { if (null == text || "".equals(text)) { return 0; } StaticLayout staticLayout = new StaticLayout(text, paint, maxWidth, .ALIGN_NORMAL , 1.0f, 0, false); //Get the character subscript displayed at the last line return (0); }
Using StaticLayout, you can easily get the maximum number of characters that a line can display.
extend:
For a single-line TextView, how do you get partial strings that are not displayed when the string exceeds one line?
After the textview sets the maximum number of lines to 1, the text exceeds the textview, and the ellipsis is displayed at the end of the textView. I want to know what the ellipsis represents
Ideas:
Assume that the width of the TextView is a specific value set in xml, such as 300dp,
(The purpose is to simplify this problem. If set to match_parent or wrap_content, the width needs to be calculated when the program is running, and directly getWidth always returns 0, which is more troublesome.)
For example, it is configured like this:
<TextView android: android:layout_width="300dp" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine="true" />
Then fill in a very long string, like this:
String str = "If you really want to hear about it, the first thing you'll probably want to know"; This will lead to incomplete display,Like this: If you really want to hear about it, the first thin... so,If you want to get the number of characters that have been displayed,Or the number of characters not displayed,Then the key is how to calculate the width of each character。 Then iterate through this string,currentnThe sum of the widths of each character,ExceedTextViewWhen width,You get the displayed number of characters。 String str = "If you really want to hear about it, the first thing you'll probably want to know"; mTextView = (TextView) findViewById(); // Calculate TextView width: the width defined in xml is 300dp, convert it to pxfloat textViewWidth = convertDpToPixel(300); float dotWidth = getCharWidth(mTextView, '.'); (TAG, "TextView width " + textViewWidth); int sumWidth = 0; for (int index=0; index<(); index++) { // Calculate the width of each character char c = (index); float charWidth = getCharWidth(mTextView, c); sumWidth += charWidth; (TAG, "#" + index + ": " + c + ", width=" + charWidth + ", sum=" + sumWidth); if (sumWidth + dotWidth*3 >= textViewWidth) { (TAG, "TextView shows #" + index + " char: " + (0, index)); break; } } // Dp to Pxprivate float convertDpToPixel(float dp){ Resources resources = getResources(); DisplayMetrics metrics = (); float px = dp * ( / 160f); return px; } // Calculate the width of each characterpublic float getCharWidth(TextView textView, char c) { ((c)); (0, 0); return (); }
The results are as follows, and the test was passed on Honor 3C and LG G3 (G3 is more than the calculation, showing one more character):
10-22 01:17:42.046: D/Text(21495): TextView width 600.0
10-22 01:17:42.048: D/Text(21495): #0: I, width=8.0, sum=8
10-22 01:17:42.049: D/Text(21495): #1: f, width=9.0, sum=17
10-22 01:17:42.049: D/Text(21495): #2: , width=7.0, sum=24
10-22 01:17:42.049: D/Text(21495): #3: y, width=14.0, sum=38
......
10-22 01:17:42.053: D/Text(21495): #17: t, width=9.0, sum=213
10-22 01:17:42.053: D/Text(21495): #18: , width=7.0, sum=220
10-22 01:17:42.053: D/Text(21495): #19: t, width=9.0, sum=229
......10-22 01:17:42.061: D/Text(21495): #50: n, width=16.0, sum=575
10-22 01:17:42.061: D/Text(21495): #51: g, width=16.0, sum=591
10-22 01:17:42.061: D/Text(21495): TextView shows #51 char: If you really want to hear about it, the first thin
This is the article about getting the most displayed textview from Android. For more related android textview content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!