正则去除html行内样式,Android-富文本处理-html字符串去掉内部样式,统一添加body、style,统一支持换行等...
最近Webview需要顯示html字符串,這個字符串是從別的網站用Python抓取過來的。需要干掉亂七八糟的樣式,然后定義成自己的樣式,圖片自適應支持(寬度充滿控件,高度自適應)、換行支持,字體大小、字體顏色、去掉背景等處理。
想法就是用正則干掉所有的style,然后逐一添加圖片自適應支持、禁止復制onselectstart、整體加個自適應換行、字體大小和顏色。
具體處理代碼:
/**
* 設置html文本 - data字符串
*
* @param hmtlData
* @2019.1.1 -支持了下Img適應屏幕寬度
*/
public void setHtml(String hmtlData) {
// || hmtlData.equals("")
if (null == hmtlData) {
return;
}
String support_img = hmtlData;
// 先把所有的style干掉 - 正則表達式
String regEx = " style=\"(.*?)\"";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(support_img);
if (m.find()) {
support_img = m.replaceAll("");
}
support_img = support_img.replace("
// body加了就行了,這里就不用再加了
//support_img = support_img.replace("
support_img = "";
// 整體加個自適應換行、字體大小和顏色
support_img = "
";// this是Webview控件哈,自行替換
this.loadDataWithBaseURL(null, support_img, "text/html", "utf-8", null);
}
搞定收工。。。
總結
以上是生活随笔為你收集整理的正则去除html行内样式,Android-富文本处理-html字符串去掉内部样式,统一添加body、style,统一支持换行等...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hadoop1.x版本升级Hadoop2
- 下一篇: 剖析Docker Swarm和Mesos