问题描述
- 在TextView中删除链接的下划线
- 程序中有两个textview来显示数据库中的链接,我想改变链接的颜色,并且把链接下划线去掉。
email.setText(c.getString(5)); website.setText(c.getString(6)); Linkify.addLinks(email Linkify.ALL); Linkify.addLinks(website Linkify.ALL);
使用XML或代码如何实现呢?在我的代码中再添加什么函数?
解决方案
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的“下划线”属性:
private class URLSpanNoUnderline extends URLSpan { public URLSpanNoUnderline(String url) { super(url); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); }}
时间: 2024-09-09 21:10:33