2014-05-14 7 views
1

J'ai du texte html que j'ai besoin d'afficher dans TextView. Le code HTML peut ressembler à ceci -Android - Html.fromHtml gérer la couleur de fond

<font color="#AFEEEE"><font style="background-color: rgb(255,140,0);">Text with background and color</font></font> 

Html.fromHtml ne supporte pas d'attribut autre que la couleur pour la balise de police. Mais nous devons absolument montrer le contexte. Je pourrais écrire un gestionnaire de balises personnalisé, mais les attributs ne sont pas passés, seulement la balise est passée. Quelle est la meilleure façon d'y parvenir?

REMARQUE: Impossible d'utiliser Webview.


J'ai essayé le code ci-dessous. Si je mets brut sur le texte, cela fonctionne, mais si je le traite plus loin et le passe à Html.fromHtml, il ne montre pas l'arrière-plan.

public static final String sText = 
    "Background on <font style=\"background-color: rgb(255,255,0);\">pa</font>rt text only"; 

     Pattern pattern = Pattern.compile(BACKGROUND_PATTERN); 
     Matcher matcher = pattern.matcher(sText); 
     SpannableString raw = new SpannableString(sText); 
     BackgroundColorSpan[] spans = 
      raw.getSpans(0, raw.length(), BackgroundColorSpan.class); 
     for (BackgroundColorSpan span : spans) { 
      raw.removeSpan(span); 
     } 

     while (matcher.find()) { 
      raw.setSpan(new BackgroundColorSpan(0xFF8B008B), 
       matcher.start(2), matcher.start(2) + matcher.group(2).length(), 
       Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
     } 
     sText = raw.toString(); 
     final Spanned convertedHtml = 
      Html.fromHtml(sText, ig, new myTagHandler()); 
+1

utilisation jsoup pour analyser le code html pour accéder à l'objet de style et la couleur d'arrière-plan, puis définir cette couleur comme arrière-plan à votre vue. – njzk2

Répondre

1

Ajoutez le vôtre BackgroundColorSpan comme bon vous semble.

Voici un code qui définit une telle durée sur toutes les occurrences d'un terme de recherche dans un TextView:

private void searchFor(String text) { 
    TextView prose=(TextView)findViewById(R.id.prose); 
    Spannable raw=new SpannableString(prose.getText()); 
    BackgroundColorSpan[] spans=raw.getSpans(0, 
              raw.length(), 
              BackgroundColorSpan.class); 

    for (BackgroundColorSpan span : spans) { 
     raw.removeSpan(span); 
    } 

    int index=TextUtils.indexOf(raw, text); 

    while (index >= 0) { 
     raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index 
      + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
     index=TextUtils.indexOf(raw, text, index + text.length()); 
    } 

    prose.setText(raw); 
    } 

Alors, trouvez votre point de départ et de fin, créez un BackgroundSpan avec la couleur désirée, et l'utilisation setSpan() pour l'appliquer.

Notez que cela suppose que seule une partie de votre texte nécessite la couleur d'arrière-plan. Si le TextView entier a besoin de la couleur, allez avec la suggestion de njzk2, et appliquez juste la couleur à l'ensemble TextView.

+0

Alors que la solution ci-dessus fonctionne, j'ai aussi besoin d'invoquer Html.fromHtml pour convertir d'autres tags comme gras, italique, etc. Donc, je ne peux pas utiliser la méthode ci-dessus avant ou même après avoir appelé Html.fromHtml. – user1018916

+0

@ user1018916: Pourquoi pas? Les intervalles peuvent chevaucher AFAIK et le code ci-dessus n'affecte que BackgroundColorSpan. – CommonsWare

+0

Je dois manquer quelque chose. Html.fromHtml accepte les chaînes. Mais si je recherche et applique les intervalles d'arrière-plan d'abord, puis le passe à fromHtml, il ne met pas dans la couleur de fond. Collé dans mon post ci-dessus. – user1018916

0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
    String str = "<span style=\"background-color:#f3f402;\">" + TEXT TO HIGHLIGHT + "</span>"; 
    textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY)); 
} else { 
    String str = "<font color='#f3f402'>" + TEXT TO HIGHLIGHT + "</font>"; 
    textView.setText(Html.fromHtml(str)); 
} 

Plus - https://stackoverflow.com/a/46035856/3625510