Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
我們平時(shí)在進(jìn)行安卓開發(fā)使用到webview加載網(wǎng)頁時(shí),我們不能準(zhǔn)確了解網(wǎng)頁的加載進(jìn)度,因此為了提高用戶體驗(yàn),我們在webview中加入進(jìn)度條顯示加載進(jìn)度。
程序預(yù)覽界面:

一、主界面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 自動(dòng)生成的方法存根
webView=(WebView) findViewById(R.id.webview1);
pg1=(ProgressBar) findViewById(R.id.progressBar1);
webView.setWebViewClient(new WebViewClient(){
//覆寫shouldOverrideUrlLoading實(shí)現(xiàn)內(nèi)部顯示網(wǎng)頁
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO 自動(dòng)生成的方法存根
view.loadUrl(url);
return true;
}
});
WebSettings seting=webView.getSettings();
seting.setJavaScriptEnabled(true);//設(shè)置webview支持javascript腳本
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO 自動(dòng)生成的方法存根
if(newProgress==100){
pg1.setVisibility(View.GONE);//加載完網(wǎng)頁進(jìn)度條消失
}
else{
pg1.setVisibility(View.VISIBLE);//開始加載網(wǎng)頁時(shí)顯示進(jìn)度條
pg1.setProgress(newProgress);//設(shè)置進(jìn)度值
}
}
});
}
//設(shè)置返回鍵動(dòng)作(防止按返回鍵直接退出程序)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO 自動(dòng)生成的方法存根
if(keyCode==KeyEvent.KEYCODE_BACK) {
if(webView.canGoBack()) {//當(dāng)webview不是處于第一頁面時(shí),返回上一個(gè)頁面
webView.goBack();
return true;
}
else {//當(dāng)webview處于第一頁面時(shí),直接退出程序
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
}
整體流程就這樣。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
- Android 進(jìn)度條 ProgressBar的實(shí)現(xiàn)代碼(隱藏、出現(xiàn)、加載進(jìn)度)
- Android自定義View仿華為圓形加載進(jìn)度條
- Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果
- Android開發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動(dòng)
- Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條
- Android自定義帶加載動(dòng)畫效果的環(huán)狀進(jìn)度條
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- Android自定義帶進(jìn)度條WebView仿微信加載過程
- Android自定義View實(shí)現(xiàn)圓形加載進(jìn)度條
相關(guān)文章
Android Jetpack架構(gòu)中ViewModel接口暴露的不合理探究
這篇文章主要介紹了Android Jetpack架構(gòu)組件 ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲(chǔ)和管理界面相關(guān)的數(shù)據(jù)。感興趣可以來學(xué)習(xí)一下2022-07-07
從0快速搭建一個(gè)實(shí)用的MVVM框架(超詳細(xì))
這篇文章主要介紹了從0搭建一個(gè)實(shí)用的MVVM框架,結(jié)合Jetpack,構(gòu)建快速開發(fā)的MVVM框架,支持快速生成ListActivity、ListFragment,主要是基于MVVM進(jìn)行快速開發(fā)上手即用,需要的朋友可以參考下2022-03-03
基于VSTS的Xamarin.Android持續(xù)集成步驟詳解
這篇文章主要介紹了基于VSTS的Xamarin.Android持續(xù)集成步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Android 安全加密:消息摘要Message Digest詳解
本文主要介紹Android安全加密消息摘要Message Digest,這里整理了詳細(xì)的資料,并說明如何使用Message Digest 和使用注意事項(xiàng),有需要的小伙伴可以參考下2016-09-09
Android組件化開發(fā)路由的設(shè)計(jì)實(shí)踐
本篇文章主要介紹了Android組件化開發(fā)路由的設(shè)計(jì)實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android自定義View中attrs.xml的實(shí)例詳解
這篇文章主要介紹了Android自定義View中attrs.xml的實(shí)例詳解的相關(guān)資料,在自定義View首先對attrs.xml進(jìn)行布局的實(shí)現(xiàn)及屬性的應(yīng)用,需要的朋友可以參考下2017-07-07
Android編程圖片操作類定義與用法示例【拍照,相冊選圖及裁剪】
這篇文章主要介紹了Android編程圖片操作類定義與用法,涉及Android拍照,相冊選圖及裁剪等圖片操作功能及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Android 提交或者上傳數(shù)據(jù)時(shí)的dialog彈框動(dòng)畫效果
我們在使用支付寶支付的時(shí)候會(huì)看到類似這種彈框動(dòng)畫效果,下面通過實(shí)例代碼給大家分享android 提交或者上傳數(shù)據(jù)時(shí)的彈框動(dòng)畫效果,感興趣的的朋友參考下2017-07-07
react native打包apk文件安裝好之后進(jìn)入應(yīng)用閃退的解決方案
這篇文章主要介紹了react native打包apk文件安裝好之后進(jìn)入應(yīng)用閃退的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

