Android基础——WebView

    xiaoxiao2021-03-25  76

    前言

    2016年12月份开始写博客,然而在年末的时候没有坚持去写,知道现在想起自己有三个月没有记录自己的技术点了,说坚持容易,但是能真正坚持下来的人有几个。能够真正坚持把自己所掌握的东西记录下来的人,后来都很不错。 乔布斯在斯坦福大学演讲时说的: Again, you can’t connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future. You have to trust in something — your gut, destiny, life, karma, whatever. This approach has never let me down, and it has made all the difference in my life. 现在的积累就是为自己将来成功打下的基础。

    WebView

    基本所有的APP都离不开网页,也就是WebView这个控件,在使用的时候有些设置一直记不住,很容易出现很多的Bug,现在我把我自己总结WebView的使用方法列出来:

    wv_webview.loadUrl(webUrl); /** * 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 */ wv_webview.setWebViewClient(new WebViewClient() { /** * 返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { /*这里我这么写,是因为在网页端跳转有时点击返回按钮网页不反回上一个网页,而是不断的加载当前这个页面。这样写当网页的url一样时跳转一个新的WebAvctivity。在点击时可以把WebAvctivity finish掉*/ // if (!webUrl.equals(url)){ // Intent intent = new Intent(); // intent.setClass(WebViewActivity.this, WebViewActivity.class); // intent.putExtra("webUrl", url); // startActivity(intent); // } view.loadUrl(url); return true; } /** * webView 开始加载 */ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } /** * webView 结束加载 */ @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); wv_webview.setVisibility(View.VISIBLE); } /** * webview 加载错误 */ @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); } }); /* LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据 LOAD_DEFAULT: 根据cache-control决定是否从网络上取数据。 LOAD_CACHE_NORMAL: API level 17中已经废弃, 从API level 11开始作用同LOAD_DEFAULT模式 LOAD_NO_CACHE: 不使用缓存,只从网络获取数据. LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。 */ wv_webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY); wv_webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); wv_webview.getSettings().setLoadsImagesAutomatically(true); wv_webview.getSettings().setJavaScriptEnabled(true);//是否允许JS // wv_webview.setVerticalScrollBarEnabled(false);//设置是否有竖着的滑动条 wv_webview.setHorizontalScrollBarEnabled(false);//设置是否有横着的滑动条 wv_webview.getSettings().setSupportZoom(false);//是否可以缩放,默认true wv_webview.getSettings().setBuiltInZoomControls(false);//是否显示缩放按钮,默认false wv_webview.getSettings().setAppCacheEnabled(true);//是否使用缓存 wv_webview.getSettings().setDomStorageEnabled(true);//开启DOM Storage API,支持本地storage缓存的网页 wv_webview.requestFocus();//触摸焦点作用 wv_webview.getSettings().setUseWideViewPort(true);//设置此属性,可以任意比例缩放,大图模式 wv_webview.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题 wv_webview.setOverScrollMode(View.OVER_SCROLL_NEVER); // 网上说这个是:禁止即在网页顶出现一个空白,又自动回去。

    其实WebView还有很多种方法没有列出来,我再遇到的时候会慢慢补全它的方法,让使用起来更加的方便些。 ��疑问:为什么我自己把WebView的一些常用的Setting设置封装起来作为一个新的Webview,在Activity里使用会失效??? 如有疑问或者想法者可以下方留言或发送邮箱wdmxzf@126.com, QQ:1078921600 加QQ请输入

    转载请注明原文地址: https://ju.6miu.com/read-32979.html

    最新回复(0)