The origin of TextView problem
TextView in Chinese and English mixed, there will be automatic break, I believe many people have met. This is a system bug, after Android5.0 was fixed, and less than 5.0 have not seen a better version of the resolution.
Referring to the online approach, some friends recommend the use of full-width and half-width conversion (not solve the problem), some recommend the Justifytextview this control (the effect is not ideal).
So I decided to customize a TextView to do this, reluctantly solve the problem, but at the cost of losing a lot of TextView own features, and TextView itself do a lot of caching and optimization work, Google strongly does not recommend that we modify this control .
Let's take a look at the implementation effect:
line 1th : Native TextView are all Chinese
Line 2nd : Native TextView all in English
Line 3rd : Native TextView, using full-width half-width conversion of Chinese and English inclusions
Line 4th : Native TextView Chinese and English inclusions
Line 5th : Justifytextview Chinese and English inclusions
Line 6th : Native adaptabletextview** Chinese and English inclusions * *
How to solve? 1. Divide text into multiple lines
The first step in thinking is to divide the text that we want to set, such as "Chinese and English inclusions Testtesttesttesttesttesttesttesttesttest", into multiple lines saved in a ArrayList () .
We can first get the width of the TextView , and then, based on this width, cut the above string into multiple segments.
Because "I" this Chinese in the drawing time, the width is larger than the single English letter, for example is "M". So we must iterate through each character to get its width.
/** * Calculates the string per line based on the width of the control */ Private void ParseText() {strs.clear ();intStart =0;//Line start index intCurlinewidth =0;//Current line width for(inti =0; I < mtext.length (); i++) {Charch = mtext.charat (i);//Get current character float[] widths =New float[1]; String srt = string.valueof (CH); Mpaint.gettextwidths (srt, widths);//Get the width of this character if(ch = =' \ n '){//If it is a newline character, when the only rowStrs.add (mtext.substring (Start, i)); Start = i +1; Curlinewidth =0; }Else{Curlinewidth + = (int) (Math.ceil (widths[0]));//Calculate current width if(Curlinewidth > Mlinewidth) {//until the current line width is greater than the width of the control, truncated to one rowStrs.add (mtext.substring (Start, i)); start = i; i--; Curlinewidth =0; }Else{if(i = = (Mtext.length ()-1)){//The remaining single rowStrs.add (mtext.substring (Start, Mtext.length ())); } } } } }
2. Use the DrawText () method to draw each line
After the above method, we get each line of string, the rest of the work is to use DrawText () to draw each line is OK. Also note that, for example, if the maxLinesis exceeded, we can draw an ellipsis manually, as well as a padding setup problem.
public void Draw (CanvasCanvas) {intlines = mmaxlines >0&& mmaxlines <= STRs.size() ? Mmaxlines:strs.size(); for(inti =0; i < lines; i++) {Stringtext= Strs.get (i);//If it is the last line of the largest row but not the last line of the true, the ellipsis is added automatically if(i = = lines-1&& i < STRs.size() -1)text=text.substring(0,text. Length ()-3) +"...";Canvas. DrawText (text, Getpaddingleft (), Getpaddingtop () +mpaint.gettextsize () + mlineheight * I, mpaint); } }
3, rewrite the TextView OnDraw () method
Simply put, just call our custom draw () method inside the OnDraw () method to draw the text.
@Override @SuppressLint("NewApi") protectedvoidonDraw(Canvas canvas) { getAdaptableText(); if(mIsDirty) { false; String text = getText().toString(); int maxLines = getMaxLines(); if(!mAdaptableText.getText().equals(text)) mAdaptableText.setText(text); if(mAdaptableText.getMaxLines() != maxLines) mAdaptableText.setMaxLines(maxLines); } mAdaptableText.draw(canvas); }
Written in the last
TextView this bug, almost all the apps exist (what), it can be seen that there seems to be no good solution.
If you use the custom control above, it seems to solve the problem, actually bring a serious decline in textview efficiency, all also strongly do not recommend to use it in the project (if your app does not pursue efficiency is another matter).
Download source code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
TextView in Chinese and English typesetting confusion