Android WebView 设置参考

Android About 8,282 words

代码

mWebView.setHorizontalScrollBarEnabled(false);//水平不显示
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 

//listview,webview中滚动拖动到顶部或者底部时的阴影
mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);
//取消滚动条白边效果
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);


WebSettings webSettings = mWebView.getSettings();
webSettings.setRenderPriority(RenderPriority.HIGH);//设置渲染优先级
webSettings.setLoadWithOverviewMode(true);//缩放至屏幕大小,一定要设置
webSettings.setJavaScriptEnabled(true);//支持JS
webSettings.setAllowFileAccess(true);//设置是否允许文件访问,默认是允许的
webSettings.setSaveFormData(true);//设置是否保存表单数据,默认是保存的
webSettings.setSavePassword(true);//设置是否保存密码,默认是保存的,过时了
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通过JS打开新窗口
webSettings.setUseWideViewPort(true);//是否启用支持视窗meta标记
webSettings.setTextSize(TextSize.NORMAL);// 固定页面字体大小
webSettings.setGeolocationEnabled(true);//启用地理定位
webSettings.setDatabaseEnabled(true);// 开启database storage API功能
webSettings.setDomStorageEnabled(true);// 开启DOM storage API 功能
String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
webSettings.setDatabasePath(databasePath);// 设置数据库缓存路径
webSettings.setGeolocationDatabasePath(databasePath);//设置定位的数据库路径
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);//支持内容重新布局
webSettings.setSupportZoom(true);//支持缩放
webSettings.setBuiltInZoomControls(true);//设置支持缩放机制
webSettings.setDisplayZoomControls(false);//设置显示缩放控件

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    //设置当一个安全站点企图加载来自一个不安全站点资源时WebView的行为
    //MIXED_CONTENT_ALWAYS_ALLOW
    //MIXED_CONTENT_NEVER_ALLOW
    //MIXED_CONTENT_COMPATIBILITY_MODE
    webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}        



//在同种分辨率的情况下,屏幕密度不一样的情况下,自动适配页面:
DisplayMetrics dm = getResources().getDisplayMetrics();
int scale = dm.densityDpi;
if (scale >= DisplayMetrics.DENSITY_HIGH) {//240
    mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);//far:240dp
} else if (scale == DisplayMetrics.DENSITY_MEDIUM) {//160
    mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);//medium:160dp
} else {
    mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);//close:120dp
}


mWebView.setOnLongClickListener(new View.OnLongClickListener() {//禁止长按跳出复制弹窗
    @Override
    public boolean onLongClick(View view) {
        return true;
    }
});

mWebView.setDownloadListener(new DownloadListener() {
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        try {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

mWebView.setWebViewClient(new WebViewClient() {

    private boolean isError = false;//signal

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
        Log.d(TAG, "zbj onReceivedSslError");
        //handler.cancel(); 默认的处理方式,WebView变成空白页
        handler.proceed();//接受证书
    }

    //WebView重定向,多次返回键无效
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        WebView.HitTestResult hitTestResult = view.getHitTestResult();
        //hitTestResult==null解决重定向问题
        if (!TextUtils.isEmpty(url) && hitTestResult == null) {
            view.loadUrl(url);
            return true;
        }
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String url) {
        isError = true;
        if (view != null){
            view.stopLoading();
            view.setVisibility(View.GONE);//NullPointerException
        }
        mErrorView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (isError) {
            isError = false;
        } else {
            if (view != null)
                view.setVisibility(View.VISIBLE);//NullPointerException
            mErrorView.setVisibility(View.GONE);
        }
    }


}

mWebView.setWebChromeClient(new WebChromeClient() {

    @Override
    public void onProgressChanged(WebView view, final int progress) {
        mProgressView.post(new Runnable() {
            @Override
            public void run() {
                mProgressView.setProgress(progress);
                if (progress <= 100) {
                    mProgressView.setVisibility(View.VISIBLE);
                }
                if (progress >= 100) {
                    mProgressView.post(new Runnable() {
                        @Override
                        public void run() {
                            mProgressView.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });

        super.onProgressChanged(view, progress);
    }

    @Override
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
        quotaUpdater.updateQuota(5 * 1024 * 1024);
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, true);
        super.onGeolocationPermissionsShowPrompt(origin, callback);
    }


}




@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (mWebView.canGoBack()) {
            mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            mWebView.stopLoading();
            mWebView.goBack();//返回上一页面
            return true;
        } else {
            finish();
        }
    }
    return super.onKeyDown(keyCode, event);
}


@Override
protected void onDestroy() {
    if (mWebView != null) {
        ((ViewGroup) mWebView.getParent()).removeView(mWebView);
        mWebView.destroy();
        mWebView = null;
    }
    super.onDestroy();
}


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return handlerScheme(url);
}


private static final String SCHEME_HTTP = "http";
private static final String SCHEME_HTTPS = "https";
private static final String SCHEME_FILE = "file";
private static final String SCHEME_TEL = "tel";
private static final String SCHEME_INTENT = "intent"; // Chrome Intent scheme;

private boolean handlerScheme(String url) {
        Uri uri = Uri.parse(url);
        String scheme = uri.getScheme();
        if (!TextUtils.isEmpty(scheme)) {
            scheme = scheme.toLowerCase(Locale.getDefault());
            if (SCHEME_TEL.equals(scheme)) {
                String number = url.substring(url.indexOf(":") + 1);
                if (!TextUtils.isEmpty(number)) {
                    Intent it = new Intent(Intent.ACTION_DIAL, Uri.parse(SCHEME_TEL + ":" + number));
                    it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(it);
                }
                return true;
            } else if (SCHEME_INTENT.equals(scheme)) {
                try {
                    Intent intent = Intent.parseUri(url, Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return true;
            } else if (!SCHEME_HTTP.equals(scheme) && !SCHEME_HTTPS.equals(scheme)) {
                try {
                    Log.d(TAG, "startActivity  uri = " + uri);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }

        }

        Log.d(TAG, "scheme = " + scheme);

        return false;
    }
Views: 5,755 · Posted: 2019-04-11

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh