android html line-breaks spanned

android - Eliminar saltos de línea adicionales después de Html.fromHtml()



line-breaks spanned (4)

Estoy tratando de colocar html en un TextView. Todo funciona perfectamente, este es mi código.

String htmlTxt = "<p>Hellllo</p>"; // the html is form an API Spanned html = Html.fromHtml(htmlTxt); myTextView.setText(html);

Esto establece mi TextView con el html correcto. Pero mi problema es tener un

En la etiqueta del HTML, el texto del resultado que se incluye en TextView tiene una "/ n" al final, por lo que aumenta la altura de mi TextView de lo que debería ser.

Dado que es una variable distribuida, no puedo aplicar la sustitución de expresiones regulares para eliminar el "/ n", y si tuviera que convertirla en una cadena, aplicar la expresión regular, pierdo la funcionalidad de tener anclajes html para funcionar correctamente.

¿Alguien sabe alguna solución para eliminar los linebreak (s) finales de una variable "Spanned"?


Buena respuesta @Christine. Escribí una función similar para eliminar los espacios en blanco finales de un CharSequence esta tarde:

/** Trims trailing whitespace. Removes any of these characters: * 0009, HORIZONTAL TABULATION * 000A, LINE FEED * 000B, VERTICAL TABULATION * 000C, FORM FEED * 000D, CARRIAGE RETURN * 001C, FILE SEPARATOR * 001D, GROUP SEPARATOR * 001E, RECORD SEPARATOR * 001F, UNIT SEPARATOR * @return "" if source is null, otherwise string with all trailing whitespace removed */ public static CharSequence trimTrailingWhitespace(CharSequence source) { if(source == null) return ""; int i = source.length(); // loop back to the first non-whitespace character while(--i >= 0 && Character.isWhitespace(source.charAt(i))) { } return source.subSequence(0, i+1); }


El spannable es un CharSequence, que puede manipular.

Esto funciona:

myTextView.setText(noTrailingwhiteLines(html)); private CharSequence noTrailingwhiteLines(CharSequence text) { while (text.charAt(text.length() - 1) == ''/n'') { text = text.subSequence(0, text.length() - 1); } return text; }


Puedes probar esto:

Spanned htmlDescription = Html.fromHtml(textWithHtml); String descriptionWithOutExtraSpace = new String(htmlDescription.toString()).trim(); textView.setText(htmlDescription.subSequence(0, descriptionWithOutExtraSpace.length()));


Puedes usar estas líneas ... funciona totalmente;)

Sé que su problema se resolvió, pero tal vez alguien lo encuentre útil.

try{ string= replceLast(string,"<p dir=/"ltr/">", ""); string=replceLast(string,"</p>", ""); }catch (Exception e) {}

y aquí está reemplazarLast ...

public String replceLast(String yourString, String frist,String second) { StringBuilder b = new StringBuilder(yourString); b.replace(yourString.lastIndexOf(frist), yourString.lastIndexOf(frist)+frist.length(),second ); return b.toString(); }