欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

除TextView超链接下划线

程序员文章站 2022-03-28 14:30:26
...

     没发现有相关的属性,google一下都是重写Span。做个记录

    private void stripUnderLines(TextView tv) {
	try {
	    Spannable text = (Spannable) tv.getText();
	    if (text == null)
		return;
	    URLSpan[] old = text.getSpans(0, text.length(), URLSpan.class);
	    if (old == null)
		return;

	    for (int i = old.length - 1; i >= 0; i--) {
		URLSpan us = old[i];
		final int start = text.getSpanStart(us), end = text.getSpanEnd(us), flags = text.getSpanFlags(us);
		text.removeSpan(us);
		text.setSpan(new MyURLSpan(us.getURL()), start, end, flags);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
    private final class MyURLSpan extends URLSpan{
	    public MyURLSpan(String url) {
	        super(url);
	    }
	    public MyURLSpan(Parcel src) {
		super(src);
	    }
	    
	    public void updateDrawState(TextPaint ds) {
		ds.setColor(ds.linkColor);
	        ds.setUnderlineText(false);
	    }
    }