从 TextView 中的链接中删除下划线 - Android [英] Remove underline from links in TextView - Android
问题描述
我使用两个 textview
来显示数据库中的链接,我设法更改了链接颜色,但我想删除下划线
I am using two textview
to display links from database, I managed to change link colors but I want to remove the underline
email.setText(c.getString(5));
website.setText(c.getString(6));
Linkify.addLinks(email, Linkify.ALL);
Linkify.addLinks(website, Linkify.ALL);
我可以从 XML 或代码中做到这一点吗?
Can I do that from XML or Code ?
推荐答案
您可以通过使用不带下划线的版本查找和替换 URLSpan
实例来在代码中完成此操作.调用 Linkify.addLinks()
后,调用下面粘贴在每个 TextView
上的函数 stripUnderlines()
:
You can do it in code by finding and replacing the URLSpan
instances with versions that don't underline. After you call Linkify.addLinks()
, call the function stripUnderlines()
pasted below on each of your TextView
s:
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
这需要不启用 TextPaint 的下划线"属性的自定义版本的 URLSpan:
This requires a customized version of URLSpan which doesn't enable the TextPaint's "underline" property:
private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
这篇关于从 TextView 中的链接中删除下划线 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!