使用字符串资源创建超链接

Create a Hyperlink using String resources

我尝试将 TextView 作为超链接。这一个按预期工作:

content.text = "<a href=${args.article.url}>Content</a>".fromHtml()

但我收到有关使用字符串资源的 lint 警告。但是这个现在确实将 TextView 显示为超链接:

content.text = getString(R.string.content, args.article.url).fromHtml()

这是字符串资源:

<string name="content"><a href="%s">Content</a></string>

是否有使用字符串资源修复它的解决方案?

@Suppress("DEPRECATION")
fun String.fromHtml() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Html.fromHtml(this, Html.FROM_HTML_MODE_COMPACT)
} else {
    Html.fromHtml(this)
}

这是在 TextView 中设置文本的方法:

content.text = TextUtils.expandTemplate(getText(R.string.content), args.article.url).toString().fromHtml()

这是设置字符串资源的方法:

<string name="content"><![CDATA[ <a href="$s">Content</a>]]></string>