Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
我們平時在進(jìn)行安卓開發(fā)使用到webview加載網(wǎng)頁時,我們不能準(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 自動生成的方法存根 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 自動生成的方法存根 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 自動生成的方法存根 if(newProgress==100){ pg1.setVisibility(View.GONE);//加載完網(wǎng)頁進(jìn)度條消失 } else{ pg1.setVisibility(View.VISIBLE);//開始加載網(wǎng)頁時顯示進(jìn)度條 pg1.setProgress(newProgress);//設(shè)置進(jìn)度值 } } }); } //設(shè)置返回鍵動作(防止按返回鍵直接退出程序) @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO 自動生成的方法存根 if(keyCode==KeyEvent.KEYCODE_BACK) { if(webView.canGoBack()) {//當(dāng)webview不是處于第一頁面時,返回上一個頁面 webView.goBack(); return true; } else {//當(dāng)webview處于第一頁面時,直接退出程序 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)度條的加載而滾動
- Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條
- Android自定義帶加載動畫效果的環(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 TableLayout數(shù)據(jù)列表的回顯清空實(shí)現(xiàn)思路及代碼
數(shù)據(jù)列表的回顯必須從后面減去子元素同時必須從后面減去子元素,感興趣的朋友可以看下具體的實(shí)現(xiàn)代碼,希望對你學(xué)習(xí)Android TableLayout有所幫助2013-04-04Android Studio IDE升級4.1以后Start Failed
這篇文章主要介紹了Android Studio IDE升級4.1以后Start Failed,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android編程實(shí)現(xiàn)基于BitMap獲得圖片像素數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)基于BitMap獲得圖片像素數(shù)據(jù)的方法,對比分析了兩種獲取圖片像素的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11Android開發(fā)實(shí)現(xiàn)ListView部分布局監(jiān)聽的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ListView部分布局監(jiān)聽的方法,結(jié)合實(shí)例形式分析了Android通過設(shè)置tag標(biāo)志位實(shí)現(xiàn)ListView部分布局監(jiān)聽的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01TextView長按復(fù)制的實(shí)現(xiàn)方法(總結(jié))
下面小編就為大家?guī)硪黄猅extView長按復(fù)制的實(shí)現(xiàn)方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android 解決sqlite無法創(chuàng)建新表的問題
這篇文章主要介紹了Android 解決sqlite無法創(chuàng)建新表的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Android OkHttp 結(jié)合php 多圖片上傳實(shí)例
本篇文章主要介紹了Android OkHttp 結(jié)合php 多圖片上傳實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05RecyclerView實(shí)現(xiàn)常見的列表菜單
這篇文章主要為大家詳細(xì)介紹了用RecyclerView實(shí)現(xiàn)移動應(yīng)用中常見的列表菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12