Android中WebView的一些簡單用法
Android中WebView的一些簡單用法
一直想寫一個關于 WebView 控件的 一些簡單運用,都沒什么時間,這次也是擠出時間寫的,里面的一些基礎知識就等有時間再更新講解一下,今天就先把項目出來做一些簡單介紹,過多的內(nèi)容可以看我的源碼,都傳到github上了。
下面是項目的效果圖:
應用用到的是 MVP 設計模式,對這種模式還不太了解的可以先自行google一下,不然項目估計會看的暈,雖然我的代碼都很簡潔的。
對于MVP 可以帶著一個思路看源碼,那就是 activity(或其他組件)通過 xxPresenter 去拿數(shù)據(jù),拿到數(shù)據(jù) 在 xxPresenter 再利用 xxIView(這是一個接口)更新數(shù)據(jù),那么activity(或其他組件)繼承 xxIView 這個接口 就可以 更新UI 了
其實 WebView 只用到一些簡單的,深入應用本篇文章會在更新!
其中:
//設置WebView的一些縮放功能點 webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setHorizontalScrollBarEnabled(false); webView.getSettings().setSupportZoom(true); //設置WebView可觸摸放大縮小 webView.getSettings().setBuiltInZoomControls(true); webView.setInitialScale(70); webView.setHorizontalScrollbarOverlay(true); //WebView雙擊變大,再雙擊后變小,當手動放大后,雙擊可以恢復到原始大小 //webView.getSettings().setUseWideViewPort(true); //提高渲染的優(yōu)先級 webView.getSettings().setRenderPriority(RenderPriority.HIGH); //允許JS執(zhí)行 webView.getSettings().setJavaScriptEnabled(true); //把圖片加載放在最后來加載渲染 //webView.getSettings().setBlockNetworkImage(true); //用WebView將字符串以HTML的形式顯示出來 //webView.loadDataWithBaseURL("fake://not/needed", <p>zzz</p>, "text/html", "utf-8", ""); //在同種分辨率的情況下,屏幕密度不一樣的情況下,自動適配頁面:
與 native 進行交互:
mWebView.addJavascriptInterface(new WebAppInterface(customView.getContext()),"Android"); public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page */ @JavascriptInterface public void showToast(String toast) { // 比如點擊 webview加載的html 片段 可以 讓應用彈出一個土司 }
這里給 webview 設置 夜間模式:
也就往 html 頁面寫入 html 標簽 "<div class="night">"
public static String buildHtmlWithCss(String html, String[] cssUrls, boolean isNightMode) { StringBuilder result = new StringBuilder(); for (String cssUrl : cssUrls) { result.append(String.format(CSS_LINK_PATTERN, cssUrl)); } if (isNightMode) { result.append(NIGHT_DIV_TAG_START); } result.append(html.replace(DIV_IMAGE_PLACE_HOLDER, DIV_IMAGE_PLACE_HOLDER_IGNORED)); if (isNightMode) { result.append(NIGHT_DIV_TAG_END); } return result.toString(); }
項目還得有待改進,等這段時間忙完就跟新!?。。?br />
項目源碼 github
相關文章
基于Android實現(xiàn)保存圖片到本地并可以在相冊中顯示出來
App應用越來越人性化,不僅界面優(yōu)美而且服務也很多樣化,操作也非常方便。通過本篇文章給大家介紹基于Android實現(xiàn)保存圖片到本地并可以在相冊中顯示出來,對android保存圖片相關知識感興趣的朋友一起學習吧2015-12-12Android中對RecyclerView Adapter封裝解析
本篇文章主要介紹了Android中對RecyclerView Adapter封裝解析。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06詳解Android中Application設置全局變量以及傳值
這篇文章主要介紹了詳解Android中Application設置全局變量以及傳值的相關資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09