1.佈局 <!-- 富文本--> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp_15" android:layout_marginRight="@dimen/dp_15" />
2.後臺給的數據:"content":"<p style=\"margin-top: 0px; margin-bottom: 0px; padding: 0px; line-height: 24px; color: rgb(51, 51, 51); text-align: justify; font-family: arial; white-space: normal; background-color: rgb(255, 255, 255);\"><span class=\"bjh-p\">對於中國移動、中國聯通、中國電信三大運營商,想必國人有很多想吐槽的地方。由於沒有其他競爭者,<span class=\"bjh-strong\" style=\"font-size: 18px; font-weight: 700;\">三家每年都能賺取豐厚的利潤,特別是中國移動,平均一天能賺幾個億。</span>賺這麼多錢,但是服務水平差,辦事效率低等等問題卻依舊很突出。還有就是停機最準時,催你繳費也及時,挖坑也多,不時給你打電話,讓你升級套餐,或者是推薦充多少送多少的活動,但最終的目的是讓你花費更多,而不是給你省錢。<span class=\"bjh-br\"></span></span></p><p style=\"margin-top: 22px; margin-bottom: 0px; padding: 0px; line-height: 24px; color: rgb(51, 51, 51); text-align: justify; font-family: arial; white-space: normal; background-color: rgb(255, 255, 255);\"><span class=\"bjh-p\">在今年3月份,工信部突然說在2019年底前可以攜號轉網了,意思就是不用換號碼,就可以換運營商了。消息一出,網友們就炸了,紛紛吐槽自己一定要換,但是總共就3家,換來換去都是他們。<span class=\"bjh-strong\" style=\"font-size: 18px; font-weight: 700;\">根據網絡調查,有近50%的人認爲電信最好,而移動卻變成了最差。三大運營商各有特點,移動就是太貴,聯通就是信號太差,而電信好像最好。</span><span class=\"bjh-br\"></span></span></p><p><img class=\"large\" src=\"https://pics3.baidu.com/feed/2fdda3cc7cd98d1089db4805a2bb460a7bec9004.jpeg?token=fffc0171bbeec976d624f70c24d4beda&s=C5166B3A0D9E44C812F090DE000040B1\"/></p><p style=\"margin-top: 26px; margin-bottom: 0px; padding: 0px; line-height: 24px; color: rgb(51, 51, 51); text-align: justify; font-family: arial; white-space: normal; background-color: rgb(255, 255, 255);\"><span class=\"bjh-p\">中國移動是成立最晚的一家運營商,但卻是發展的最快,規模最大的一家,目前擁有9.2億用戶,是最多的一個。不得不說,<span class=\"bjh-strong\" style=\"font-size: 18px; font-weight: 700;\"(截取了部分)
3.代碼 //比較愛錢,代碼都是錢的符號 protected <T extends View> T ¥(int id) { return (T) super.findViewById(id); }
mWebView = (WebView) ¥(R.id.webview);
//請求道數據後:url是富文本content內容 initWebView(url, mWebView);
public static void initWebView(String url, WebView mWebView) { //設置 webView mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);//取消滾動條 mWebView.getSettings().setSupportZoom(false);//不支持縮放功能 mWebView.loadData(url, "text/html", "UTF-8"); //這裏我們使用 jsoup 修改 img 的屬性: final Document doc = Jsoup.parse(url); final Elements imgs = doc.getElementsByTag("img"); for (int i = 0; i < imgs.size(); i++) { //寬度填充手機,高度自適應 imgs.get(i).attr("style", "width: 100%; height: auto;"); } //這裏我們使用 jsoup 修改 embed 的屬性: Elements embeds = doc.getElementsByTag("embed"); for (Element element : embeds) { //寬度填充手機,高度自適應 element.attr("width", "100%").attr("height", "auto"); } //webview 無法正確識別 embed 爲視頻,所以這裏把這個標籤改成 video 手機就可以識別了 doc.select("embed").tagName("video"); mWebView.loadDataWithBaseURL(null, doc.toString(), "text/html", "UTF-8", null); }
4.效果展示