100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android WebView加载网页进度监听

Android WebView加载网页进度监听

时间:2022-06-20 05:57:24

相关推荐

Android WebView加载网页进度监听

首先是布局R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android" xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sportsii.webviewdemo.MainActivity"> <LinearLayout android:id="@+id/webView_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="3dp" android:progressDrawable="@drawable/progressbar_carch" /> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </LinearLayout> <TextView android:id="@+id/error_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="网络连接错误" android:textSize="30dp" android:visibility="gone" /></RelativeLayout>

然后是 ProgressBar样式

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="/apk/res/android" > <item android:id="@android:id/background"> <shape> <corners android:radius="2.0dip" /> <gradientandroid:angle="270.0"android:centerColor="#FFFFFF"android:centerY="2.0"android:endColor="#FFFFFF"android:startColor="#FFFFFF" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape><corners android:radius="2.0dip" /><gradientandroid:angle="270.0"android:centerColor="#8bc34a"android:centerY="2.0"android:endColor="#8bc34a"android:startColor="#8bc34a" /> </shape> </clip> </item></layer-list>

最后是逻辑代码(如果加载失败会显示加载失败的界面)

package com.example.sportsii.webviewdemo;import android.graphics.Bitmap;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.LinearLayout;import android.widget.ProgressBar;import android.widget.TextView;public class MainActivity extends AppCompatActivity {private LinearLayout webLLayout; private TextView errorTv; private WebView webView; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webView); progressBar = (ProgressBar) findViewById(R.id.progressBar); webLLayout = (LinearLayout) findViewById(R.id.webView_layout); errorTv = (TextView) findViewById(R.id.error_tv); initViews(); }private void initViews() {//支持javascript webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);// 支持通过JS打开新窗口 // 设置可以支持缩放 webView.getSettings().setSupportZoom(true); // 设置出现缩放工具 webView.getSettings().setBuiltInZoomControls(true); //扩大比例的缩放 webView.getSettings().setUseWideViewPort(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setLoadsImagesAutomatically(true);// 支持自动加载图片 //自适应屏幕 webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); webView.getSettings().setLoadWithOverviewMode(true); webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); webView.setWebChromeClient(new WebChromeClient() {@Override public void onProgressChanged(WebView view, int newProgress) {super.onProgressChanged(view, newProgress);progressBar.setProgress(newProgress); }}); webView.setWebViewClient(new WebViewClient() {@Override public void onPageStarted(WebView view, String url, Bitmap favicon) {super.onPageStarted(view, url, favicon);progressBar.setVisibility(View.VISIBLE); }@Override public void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);progressBar.setVisibility(View.GONE); }//请求失败时显示失败的界面 @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {super.onReceivedError(view, errorCode, description, failingUrl);webLLayout.setVisibility(View.GONE);errorTv.setVisibility(View.VISIBLE); }}); webView.loadUrl(" /mchenys/article/details/49930739"); }// 改写手机物理按键--返回的逻辑 @Override public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if (webView.canGoBack()) {webView.goBack();// 返回上一页面return true; } else {finish();// System.exit(0);// 退出程序 }}return super.onKeyDown(keyCode, event); }}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。