爸爸去哪儿5新春特辑,晋中市政府网,扎兰屯市政府
我们平时在进行安卓开发使用到webview加载网页时,我们不能准确了解网页的加载进度,因此为了提高用户体验,我们在webview中加入进度条显示加载进度。
程序预览界面:
一、主界面xml布局文件
<linearlayout 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" tools:context=".mainactivity" android:orientation="vertical" > <relativelayout android:layout_width="match_parent" android:layout_height="40dp" android:background="#1b9a16" /> <progressbar android:id="@+id/progressbar1" style="?android:attr/progressbarstylehorizontal" android:layout_width="match_parent" android:layout_height="3dip" android:progressdrawable="@drawable/pg" android:visibility="gone" /> <webview android:id="@+id/webview1" android:layout_below="@id/progressbar1" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
二、progressbar样式布局文件(pg.xml放在drawable下面)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background"> <shape> <corners android:radius="2dp" /> <gradient android:angle="270" android:centercolor="#e3e3e3" android:endcolor="#e6e6e6" android:startcolor="#c8c8c8" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="2dp" /> <gradient android:centercolor="#4aea2f" android:endcolor="#31ce15" android:startcolor="#5fec46" /> </shape> </clip> </item> </layer-list>
三、逻辑代码
package com.example.webview; import android.os.bundle; import android.app.activity; import android.transition.visibility; import android.view.keyevent; import android.view.menu; import android.view.view; import android.view.window; import android.webkit.webchromeclient; import android.webkit.websettings; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.progressbar; public class mainactivity extends activity { private webview webview; private progressbar pg1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); init(); webview.loadurl("http://www.baidu.com"); } private void init() { // todo 自动生成的方法存根 webview=(webview) findviewbyid(r.id.webview1); pg1=(progressbar) findviewbyid(r.id.progressbar1); webview.setwebviewclient(new webviewclient(){ //覆写shouldoverrideurlloading实现内部显示网页 @override public boolean shouldoverrideurlloading(webview view, string url) { // todo 自动生成的方法存根 view.loadurl(url); return true; } }); websettings seting=webview.getsettings(); seting.setjavascriptenabled(true);//设置webview支持javascript脚本 webview.setwebchromeclient(new webchromeclient(){ @override public void onprogresschanged(webview view, int newprogress) { // todo 自动生成的方法存根 if(newprogress==100){ pg1.setvisibility(view.gone);//加载完网页进度条消失 } else{ pg1.setvisibility(view.visible);//开始加载网页时显示进度条 pg1.setprogress(newprogress);//设置进度值 } } }); } //设置返回键动作(防止按返回键直接退出程序) @override public boolean onkeydown(int keycode, keyevent event) { // todo 自动生成的方法存根 if(keycode==keyevent.keycode_back) { if(webview.cangoback()) {//当webview不是处于第一页面时,返回上一个页面 webview.goback(); return true; } else {//当webview处于第一页面时,直接退出程序 system.exit(0); } } return super.onkeydown(keycode, event); } }
整体流程就这样。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论