WebView的載入進度,報錯處理,配置
摘要:
WebView的載入進度,報錯處理,配置
xml佈局程式碼(僅供參考):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http...
WebView的載入進度,報錯處理,配置
xml佈局程式碼(僅供參考):
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <ImageView android:id="@+id/iv_img" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="invisible" /> <com.yiban1314.yiban.widget.NumberProgressBar android:id="@+id/npb_bar" android:layout_width="match_parent" android:layout_height="wrap_content" app:max="100" /> <WebView android:id="@+id/wv_main" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> <include layout="@layout/layout_net_error_base"/> </FrameLayout>
進度條監聽
wvMain.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { LogUtils.i(newProgress+""); if (null != npbBar) { npbBar.setProgress(newProgress); if (newProgress == 100) { npbBar.setVisibility(View.GONE); wvMain.setVisibility(View.VISIBLE); } } super.onProgressChanged(view, newProgress); } });
報錯處理
wvMain.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // 使用當前WebView處理跳轉 view.loadUrl(url); // true表示此事件在此處被處理,不需要再廣播 return true; } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); //錯誤佈局切換程式碼 loadError(); } });
webView配置
private void webViewSetting() { wvMain.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//支援通過Javascript開啟新視窗 wvMain.getSettings().setJavaScriptEnabled(true);//設定WebView屬性,能夠執行Javascript指令碼 wvMain.getSettings().setDomStorageEnabled(true);//設定是否啟用了DOM Storage API wvMain.getSettings().setDatabaseEnabled(true);//開啟database storage API功能 wvMain.getSettings().setUseWideViewPort(true);//將圖片調整到適合webview的大小 wvMain.getSettings().setLoadWithOverviewMode(true);// 縮放至螢幕的大小 }