赞
踩
Textview textview = new TextView(this);
String text = textview .getText().toString().trim();
SpannableString spannableString = new SpannableString(text);
spannableString .setSpan(0x516111, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString .setSpan(new UnderlineSpan(), startIndex, endIndex, , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview .setText(spannableString );
注:在完成上面工作后,我们会发现并不能满足我们的需求,两者只能选择其中之一去显示,这样以来通过textview去实现就有些行不通。那我们只能选择后者去实现了
主要有两个步骤:
/** *source :我们要展示的文本内容 */ private String formatHtml(String source) { if (!TextUtils.isEmpty(source) && source.indexOf("http") != -1) { String link = source.substring(source.indexOf("http"), source.indexOf("html") + 4); String startContent = source.substring(0, source.indexOf("http")); String endContent = source.substring(source.indexOf("html") + 4); link = "<a style=\"color:blue;\" href=\"" + link + "\">" + link + "</a>"; source = startContent + link + endContent; } source = String.format("<div id=\"webview_content_wrapper\">%s</div>", source); String content = "<html>" + "<head>\n" + "<style type=\"text/css\">body{}</style>" + "</head>" + "<body style=\"font-family:arial;color:white;font-size:22px;text-align:justify\">" + "<script type='text/javascript'> window.onload = function(){\n" + "var $img = document.getElementsByTagName('img');\n" + "for(var p in $img){\n" + "if($img[p].width > " + width + " ) { \n" + "$img[p].style.width = '100%';\n" + "$img[p].style.height = 'auto'}}}" + "</script>" + "<p>" + source + "</p>" + "</body>" + "</html>"; return content; }
if (webView!= null) {
webView.loadData( formatHtml(text), "text/html; charset=UTF-8", null);
}
注:在这里为什么我没有使用下面的api去加载显示,原因为使用它会导致页面数据没有实时更新,我怀疑可能是加载缓存数据,具体原因没有找到,因为在这里花了时间,最后也是无奈才选择loadData()去加载,没想到还成功了
webView.loadDataWithBaseURL(params);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。