一.
A. 問(wèn)題:
在使用WebView加載某些富文本時(shí),相信很多小伙伴會(huì)遇到 大大小小不同的坑。樓主在最近的一次加載時(shí),碰到了這樣一串?dāng)?shù)據(jù):
<div class="ETab" id="detail" style="margin: 0px; padding: 0px; color: rgb(102, 102, 102); font-family: tahoma, arial, &quot;Microsoft YaHei&quot;, &quot;Hiragino Sans GB&quot;, u5b8bu4f53, sans-serif; font-size: 12px;"><div class="tab-con" style="margin: 0px; padding: 10px 0px; zoom: 1;"><div data-tab="item" style="margin: 0px; padding: 0px;"><div class="detail-content clearfix" data-name="z-have-detail-nav" style="margin: 10px 0px; padding: 0px; position: relative; background: rgb(247, 247, 247);"><div class="detail-content-wrap" style="margin: 0px; padding: 0px; width: 990px; float: left; background-color: rgb(255, 255, 255);"><div class="detail-content-item" style="margin: 0px; padding: 0px; width: 990px;"><div id="J-detail-content" style="margin: 0px; padding: 0px;"><div class="content_tpl" style="margin: 0px auto; padding: 0px; width: 750px;"><div class="formwork" style="margin: 0px; padding: 10px 0px; overflow: hidden; width: 750px; border-bottom: 1px dashed rgb(230, 230, 230); line-height: 23px; font-family: Arial, Helvetica, sans-serif; font-size: 14px;"><div class="formwork_img" style="margin: 0px auto; padding: 0px; width: 750px; text-align: center;"><img class="" src="https://img20.360buyimg.com/vc/jfs/t3322/308/666468400/253460/ea05519/5810455fN8ca588f4.jpg" style="margin: 0px; padding: 0px;"></div></div><div class="formwork" style="margin: 0px; padding: 10px 0px; overflow: hidden; width: 750px; border-bottom: 1px dashed rgb(230, 230, 230); line-height: 23px; font-family: Arial, Helvetica, sans-serif; font-size: 14px;"><div class="formwork_img" style="margin: 0px auto; padding: 0px; width: 750px; text-align: center;"><img class="" src="https://img20.360buyimg.com/vc/jfs/t3301/205/345987272/276476/11c914f5/57b28127N89002f00.jpg" style="margin: 0px; padding: 0px;"></div></div><div class="formwork" style="margin: 0px; padding: 10px 0px; overflow: hidden; width: 750px; border-bottom: 1px dashed rgb(230, 230, 230); line-height: 23px; font-family: Arial, Helvetica, sans-serif; font-size: 14px;"><div class="formwork_img" style="margin: 0px auto; padding: 0px; width: 750px; text-align: center;"><img class="" src="https://img20.360buyimg.com/vc/jfs/t2665/12/4218601922/90190/1dc1f153/57b27d73Nadf0c826.jpg" style="margin: 0px; padding: 0px;"></div></div><div class="formwork" style="margin: 0px; padding: 10px 0px; overflow: hidden; width: 750px; border-bottom: 1px dashed rgb(230, 230, 230); line-height: 23px; font-family: Arial, Helvetica, sans-serif; font-size: 14px;"><div class="formwork_img" style="margin: 0px auto; padding: 0px; width: 750px; text-align: center;"><img class="" src="https://img20.360buyimg.com/vc/jfs/t2770/244/4231824541/209550/e2845e80/57b2a6ecNa7888a5f.jpg" style="margin: 0px; padding: 0px;"></div></div><div class="formwork" style="margin: 0px; padding: 10px 0px; overflow: hidden; width: 750px; border-bottom: 1px dashed rgb(230, 230, 230); line-height: 23px; font-family: Arial, Helvetica, sans-serif; font-size: 14px;"><div class="formwork_img" style="margin: 0px auto; padding: 0px; width: 750px; text-align: center;"><img class="" src="https://img20.360buyimg.com/vc/jfs/t3184/324/349689963/150800/87c21426/57b28167Nc12541a0.jpg" style="margin: 0px; paddin
以< ;div開(kāi)頭,而不是HTML代碼通用的<div。
起初碰到會(huì)覺(jué)得驚詫,還有這種操作,經(jīng)過(guò)了解,原來(lái)這些只是某些HTML標(biāo)簽的轉(zhuǎn)義字符。在正常的使用中,我們直接用WebView加載會(huì)出現(xiàn)下圖:

false.png
B. 解決方案:只需將你所獲得的帶有< ;的HTML代碼調(diào)用以下方法,再進(jìn)行加載,即可正常顯示。
private String translation(String content) {
String replace = content.replace("<", "<");
String replace1 = replace.replace(">", ">");
String replace2 = replace1.replace("&", "&");
String replace3 = replace2.replace(""", "\"");
return replace3.replace("©", "?");
}

true.png
二.相信細(xì)心的小伙伴已經(jīng)看出來(lái)了,此方法只是做了某些轉(zhuǎn)譯字符的替換,若是有需要,還可替換更多字符??蓞㈤啠?a target="_blank" rel="nofollow">http://jiangyongyuan.iteye.com/blog/393711 進(jìn)行方法的更新與替換。
三.隨手分享,喜歡的朋友可以關(guān)注微信公眾號(hào)MiHomes,后續(xù)會(huì)有更多更好的博客推送給您。
另:歡迎指出不足,會(huì)進(jìn)行更正
末尾:移動(dòng)互聯(lián)&人力資源交流群,可加微信zy666128入群交流。

image.png