Methods a
A method used at work, although not particularly accurate, but the effect is good, here to share.
@param maxWidth @param maxWidth @param maxWidth @param maxWidth @param maxWidth @return */ private float getLineMaxNumber(String text, float size,float maxWidth) { if (null == text || "".equals(text)){ return 0; } Paint paint = new Paint(); paint.setTextSize(size); Float textWidth = paint. MeasureText (text); float textWidth = paint. // textWidth float width = textWidth / text.length(); float total = maxWidth / width; return total; }Copy the code
This method is not precise, but it is suitable for 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 (after the textView measurement is complete) ** @param text text content * @param paint textView.getPaint () * @param maxWidth Private int getLineMaxNumber(String text, TextPaint paint, int maxWidth) { if (null == text || "".equals(text)) { return 0; } StaticLayout StaticLayout = new StaticLayout(text, paint, maxWidth, layout.alignment.ALIGN_NORMAL, 1.0f, 0, false); Return statiClayout.getLineEnd (0); }Copy the code
With StaticLayout, it is very easy to get the maximum number of characters that can be displayed in a row
Extension:
For a single-line TextView, how do I get the undisplayed part of the string when the string exceeds a line?
When textView sets the maximum number of lines to 1, the text goes out of textView, and textView displays an ellipsis at the end, I want to know what the ellipsis stands for
Ideas:
Suppose the TextView width is set to a specific value in the XML, such as 300dp. (To simplify this problem, if you set it to match_parent or wrap_content, you need to calculate the width at runtime.) Direct getWidth always returns 0, which is troublesome.
For example, it is configured like this:
<TextView
android:id="@+id/textView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true" />
Copy the code
Then it fills in an extremely 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 cause the display to be incomplete, like this: If you really want to hear about it, the first thin... So, if you want the number of characters that are displayed, or the number of characters that are not displayed, the key is how to calculate the width of each character. We then iterate through the string, and when the current n character widths are over the width of the TextView, we get the number of characters that have been displayed. String str = "If you really want to hear about it, the first thing you'll probably want to know"; mTextView = (TextView) findViewById(R.id.textView); Float textViewWidth = convertDpToPixel(300); float textViewWidth = convertDpToPixel(300); float dotWidth = getCharWidth(mTextView, '.'); Log.d(TAG, "TextView width " + textViewWidth); int sumWidth = 0; for (int index=0; index<str.length(); Index ++) {// calculate the width of each character char c = str.charat (index); float charWidth = getCharWidth(mTextView, c); sumWidth += charWidth; Log.d(TAG, "#" + index + ": " + c + ", width=" + charWidth + ", sum=" + sumWidth); if (sumWidth + dotWidth*3 >= textViewWidth) { Log.d(TAG, "TextView shows #" + index + " char: " + str.substring(0, index)); break; }} private float convertDpToPixel(float Dp){Resources Resources = getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi / 160f); return px; } public float getCharWidth(TextView TextView, char c) {textView.settext (string.valueof (c)); textView.measure(0, 0); return textView.getMeasuredWidth(); }Copy the code
The results are as follows, passing the test on honor 3C and LG G3 (G3 shows one more character than calculated) :
10-22 01:17:42.048: 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, 10-22 01:17:42 sum = 24, 049: D/Text (21495) : # 3: y, width = 14.0, the 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, 22 01:17:42 sum = 10-220. 053: D/Text (21495) : # 19: t, width = 9.0, the 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 thinCopy the code