Android 实现带进度条的WebView的实例
程序员文章站
2023-11-21 18:30:58
android 实现带进度条的webview的实例
1. webview加载网页方法
//加载本地资源
loadurl("file:///android_as...
android 实现带进度条的webview的实例
1. webview加载网页方法
//加载本地资源 loadurl("file:///android_asset/example.html"); //加载网络资源 loadurl("http://baidu.com");
2. 带进度的drawable文件view_progress_webview
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#31ce15"/> <corners android:radius="2dp"/> </shape> </clip> </item> </layer-list>
颜色值为进度颜色,根据需要更换
3. progresswebview类
/** * 带进度条的webview * @author gq */ public class progresswebview extends webview { private progressbar progressbar; private context mcontext; public progresswebview(context context, attributeset attrs) { super(context, attrs); this.mcontext = context; progressbar = new progressbar(context, null, android.r.attr.progressbarstylehorizontal); progressbar.setlayoutparams(new layoutparams(layoutparams.fill_parent, 5, 0, 0)); drawable drawable = context.getresources().getdrawable(r.drawable.view_progress_webview); progressbar.setprogressdrawable(drawable); addview(progressbar); //主要处理解析,渲染网页等浏览器做的事情 setwebviewclient(new webviewclient() { @override public void onreceivederror(webview view, int errorcode, string description, string failingurl) { super.onreceivederror(view, errorcode, description, failingurl); //加载失败时候,显示自定义的页面 if (errorlistener != null) { errorlistener.onerror(); } } }); //辅助webview处理javascript的对话框,网站图标,网站title,加载进度等 setwebchromeclient(new webchromeclient()); getsettings().setsupportzoom(true);//是否可以缩放 getsettings().setbuiltinzoomcontrols(true); getsettings().setjavascriptenabled(true);//支持js getsettings().setlayoutalgorithm(websettings.layoutalgorithm.narrow_columns); getsettings().setusewideviewport(true); getsettings().setloadwithoverviewmode(true); getsettings().setsaveformdata(true); getsettings().setdomstorageenabled(true); //优先使用缓存 getsettings().setcachemode(websettings.load_cache_else_network); //禁用长按 setonlongclicklistener(new onlongclicklistener() { @override public boolean onlongclick(view view) { return true; } }); //如果在浏览器下载,调用浏览器默认下载+通知栏 setdownloadlistener(new downloadlistener() { @override public void ondownloadstart(string url, string useragent, string contentdisposition, string mimetype, long contentlength) { uri uri = uri.parse(url); intent intent = new intent(intent.action_view, uri); mcontext.startactivity(intent); } }); } public class webchromeclient extends android.webkit.webchromeclient { @override public void onprogresschanged(webview view, int newprogress) { if (newprogress == 100) { progressbar.setvisibility(gone); } else { if (progressbar.getvisibility() == gone) progressbar.setvisibility(visible); progressbar.setprogress(newprogress); } super.onprogresschanged(view, newprogress); } @override public void onreceivedtitle(webview view, string title) { super.onreceivedtitle(view, title); if (titlelistener != null) titlelistener.gettitle(title); } } @override protected void onscrollchanged(int l, int t, int oldl, int oldt) { layoutparams lp = (layoutparams) progressbar.getlayoutparams(); lp.x = l; lp.y = t; progressbar.setlayoutparams(lp); super.onscrollchanged(l, t, oldl, oldt); } private titlelistener titlelistener; public interface titlelistener { void gettitle(string title); } public void setontitlelistener(titlelistener titlelistener) { this.titlelistener = titlelistener; } private errorlistener errorlistener; public interface errorlistener { void onerror(); } public void setonerrorlistener(errorlistener errorlistener) { this.errorlistener = errorlistener; } }
4. 使用
/** * 公共webview */ public class basicwebactivity extends activity { protected progresswebview progresswebview; private textview title;//标题栏 private textview tv_none;//加载失败显示文字 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.common_webview); title = (textview) findviewbyid(r.id.title); tv_none = (textview) findviewbyid(r.id.tv_none); progresswebview = (progresswebview) findviewbyid(r.id.progresswebview); string url = getintent().getstringextra("url"); progresswebview.setontitlelistener(new progresswebview.titlelistener() { @override public void gettitle(string title) { title.settext(title); } }); progresswebview.setonerrorlistener(new progresswebview.errorlistener() { @override public void onerror() { tv_none.settext("url资源失效"); } }); //加载网页 progresswebview.loadurl(url); } //重写返回键 @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { if (progresswebview.cangoback()) { progresswebview.goback();//返回上一层页面 return true; } else { finish();//关闭页面 } } return super.onkeydown(keycode, event); } }
其中common_webview就包含一个title一个progresswebview就不贴代码了。
如有疑问,请留言或者到本站社区交流讨论,本站关于android开发的文章还有很多,还希望大家搜索参阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!