从TextView的链接删除下划线 - 机器人 [英] 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或code?
Can I do that from XML or Code ?
推荐答案
您可以通过寻找和版本替换 URLSpan
情况下做到这一点在code表示不要T下划线。在调用 Linkify.addLinks()
,调用函数 stripUnderlines()
下面贴在你的每一个的TextView
S:
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 = (Spannable)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);
}
这需要一个定制版本URLSpan的不启用TextPaint的下划线属性:
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的链接删除下划线 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!