Explain in detail the solution of Android TextView attribute ellipsize multi line failure

  • 2021-09-16 08:03:09
  • OfStack

This paper introduces the solution to the multi-line failure of Android TextView attribute ellipsize, and shares it with you as follows:

Conventional practice of displaying ellipsis in redundant text

android: ellipsize= "end"//The ellipsis is displayed at the end

android: ellipsize= "middle"//The ellipsis is shown in the middle

However, after setting android: maxLines = "2", the value of ellipsize end is valid and the value of middle is invalid. This method solves the problem that middle is invalid


/**
  *  The string is displayed to the textView, textView maxLines=2
  *  If the string is too long to display, it is replaced by ellipsis 
  *  The position of ellipsis is in the first 1 End of line 
  *
  * @param textView  That displays the string view
  * @param str   String to display 
  * @param width  That displays the string view Width of 
  * @return  String with ellipsis after processing 
  */
 private String ellipsizeString(TextView textView, String str, int width) {
  Paint paint = textView.getPaint();
  // The total width of text is less than 2 Multiple view Wide, indicating less than 2 Row, returning directly 
  if (paint.measureText(str) < 2 * width) {
   return str;
  }

  // Store and display to view Every line of text of 
  List<String> list = new ArrayList<>();

  int len = 0;
  int start, end = 0;

  while (len < str.length()) {
   len += end;
   int count = paint.breakText(str, end, str.length(), true, width, null);
   start = end;
   end = end + count;
   list.add(str.substring(start, end));
  }

  // No. 1 1 End of line text 3 Characters replaced by ellipsis 
  String line1 = list.get(0);
  line1 = line1.substring(0, line1.length() - 3) + "...";

  // Finally 1 Half-line text is truncated from the end to the front 1 Line text 
  String endLine = list.get(list.size() - 1);
  int endLineWidth = (int) paint.measureText(endLine);
  String minorEndLine = list.get(list.size() - 2);
  int minorCuteCount = paint.breakText(minorEndLine, 0, minorEndLine.length(), true, endLineWidth, null);
  String line2 = minorEndLine.substring(minorCuteCount, minorEndLine.length()) + endLine;

  return line1 + line2;
 }

The core method of code


paint.measureText(str) // Measure the width of a string  
paint.breakText(str, end, str.length(), true, width, null); // Calculates the number of strings displayed at the specified width  

Are api methods, see the source code for details

Where it needs to be improved, Different characters of android devices display different widths, Three letters are wider than three points of ellipsis, and m characters are wider than l characters. Therefore, the string processed by the above method cannot make ellipsis display to the end of the first line. If necessary, it can be accurately processed in the line code that replaces ellipsis. The idea can be made by using the above two core methods to make a series of judgments.


Related articles: