Android中WebView加载网页设置进度条

发布时间:2020-09-29 17:58:08 作者:小小小程序元
来源:脚本之家 阅读:139

我们平时在进行安卓开发使用到webview加载网页时,我们不能准确了解网页的加载进度,因此为了提高用户体验,我们在webview中加入进度条显示加载进度。

程序预览界面:

Android中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" 
   
  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); 
 } 
  
 
} 

整体流程就这样。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. WebView加载网页(二)
  2. WebView加载网页(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 加载 进度条

上一篇:vue ssr服务端渲染(小白解惑)

下一篇:Tensorflow实现酸奶销量预测分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》