【问题标题】:Change style of html links in TextViews after executing Html.fromHtml执行 Html.fromHtml 后更改 TextViews 中 html 链接的样式
【发布时间】:2011-11-14 13:08:42
【问题描述】:

我正在开发一个安卓应用程序。我检索了一些看起来像这样的数据:

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

我将它应用到这样的 TextView:

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

我在这里遇到的问题是 Html.fromHtml 似乎应用了一般样式

到任何和所有链接,即将它们涂成蓝色并加下划线。我宁愿不让它这样做,是否有任何简单的解决方案可以使它不样式化链接(因此,我假设“字体颜色=任何”将适用)?如果 HTML 链接标签位于字体/样式标签的内部,则行为不会改变。

【问题讨论】:

    标签: java android textview html-parsing text-styling


    【解决方案1】:

    使用android:textColorLink 属性。恐怕这是更改链接颜色的唯一方法。

    如果您确定文本中只有一个链接,则可以执行以下操作:

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

    【讨论】:

    • 我可以稍微修改一下以满足我的需要并使用它。就可以了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2020-02-27
    • 2017-03-06
    相关资源
    最近更新 更多