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());
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