开发者

Change style of html links in TextViews after executing Html.fromHtml

开发者 https://www.devze.com 2023-04-04 04:44 出处:网络
I\'m developing an android application. I retrieve some data that looks like this: <a href=\"http://google.com/\" title=\'\'><b><font color=\"gold\">My Link to Google!</font>&

I'm developing an android application. I retrieve some data that looks like this:

<a href="http://google.com/" title=''><b><font color="gold">My Link to Google!</font></b></a>

I'm applying it to a TextView like this:

myTextView.setText(Html.fromHtml(myHtmlString));

The issue I encounter here is that Html.fromHtml seems to apply a general styling

Change style of html links in TextViews after executing Html.fromHtml

to any and 开发者_StackOverflow社区all links, which is to color them blue and underline them. I'd rather not have it do this, is there any simple solution to make it not stylize links(and therefore, I assume, "font color=whatever" would apply instead)? The behavior does not change if the HTML link tag is on the inside of font/style tags.


Use android:textColorLink attribute. I'm afraid it's the only way to change link color.

If you're sure that you have only one link in the text then you can do the following:

Spanned text = Html.fromHtml(myHtmlString);
ForegroundColorSpan spans[] = text.getSpans(0, text.length(),
    ForegroundColorSpan.class);
if (spans.length > 0) {
    myTextView.setLinkTextColor(spans[0].getForegroundColor());
}
myTextView.setText(text);
0

精彩评论

暂无评论...
验证码 换一张
取 消