当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中替换WebView加载网页失败时的页面

Android中替换WebView加载网页失败时的页面

2019年07月24日  | 移动技术网移动技术  | 我要评论

我们用webview去请求一个网页链接的时候,如果请求网页失败或无网络的情况下,它会返回给我们这样一个页面,如下图所示:

这里写图片描述 

上面这个页面就是系统自带的页面,你觉得是不是很丑?反正小编本人觉得非常丑,很难看,于是乎小编就在想能不能自定义一个页面,当数据请求失败时让系统来加载我们自定义好的页面?上网查了很多资料,都没有关于这个问题的解决方法(反正我是没有找到),经过小编的不断琢磨,今天终于实现了这个功能。以下就是本人自定义实现的数据加载失败时的页面:

这里写图片描述 

这样看起来是不是觉得很高大尚。这和我们真正拿到数据接口做出来的效果完全一样。对于用户来说这样的体验也是很完美的。

**全部代码:

一、主代码:**

mainactivity.java

package com.example.webview;
import android.support.v4.view.viewpager;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.webkit.websettings;
import android.webkit.webview;
import android.webkit.webviewclient;
import android.widget.linearlayout;
import android.widget.relativelayout;
public class mainactivity extends appcompatactivity {
  private webview webview;
  private websettings mwebsettings;
  private view merrorview;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    webview = (webview) findviewbyid(r.id.main_webview);
    setupview();
  }
  private void setupview() {
    //加载需要显示的网页
    webview.loadurl("http://www.baidu.com/");
    //设置webview属性,能够执行javascript脚本
    webview.getsettings().setjavascriptenabled(true);
    mwebsettings = webview.getsettings();
    mwebsettings.setjavascriptenabled(true);  //允许加载javascript
    mwebsettings.setsupportzoom(true);     //允许缩放
    mwebsettings.setbuiltinzoomcontrols(true); //原网页基础上缩放
    mwebsettings.setusewideviewport(true);   //任意比例缩放
    webview.setwebviewclient(webclient); //设置web视图
  }
  /***
   * 设置web视图的方法
   */
  webviewclient webclient = new webviewclient(){//处理网页加载失败时
    public void onreceivederror(webview view, int errorcode, string description, string failingurl) {
      showerrorpage();//显示错误页面
    };
  };
  boolean miserrorpage;
  protected void showerrorpage() {
    linearlayout webparentview = (linearlayout)webview.getparent();
    initerrorpage();//初始化自定义页面
    while (webparentview.getchildcount() > 1) {
      webparentview.removeviewat(0);
    }
    @suppresswarnings("deprecation")
    linearlayout.layoutparams lp = new linearlayout.layoutparams(viewpager.layoutparams.fill_parent, viewpager.layoutparams.fill_parent);
    webparentview.addview(merrorview, 0, lp);
    miserrorpage = true;
  }
  /****
   * 把系统自身请求失败时的网页隐藏
   */
  protected void hideerrorpage() {
    linearlayout webparentview = (linearlayout)webview.getparent();
    miserrorpage = false;
    while (webparentview.getchildcount() > 1) {
      webparentview.removeviewat(0);
    }
  }
  /***
   * 显示加载失败时自定义的网页
   */
  protected void initerrorpage() {
    if (merrorview == null) {
      merrorview = view.inflate(this, r.layout.activity_error, null);
      relativelayout layout = (relativelayout)merrorview.findviewbyid(r.id.online_error_btn_retry);
      layout.setonclicklistener(new view.onclicklistener() {
        public void onclick(view v) {
          webview.reload();
        }
      });
      merrorview.setonclicklistener(null);
    }
  }
}

二、xml布局代码:

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.webview.mainactivity">
  <webview
    android:id="@+id/main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </webview>
</linearlayout>

2.activity_error.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <relativelayout
    android:id="@+id/online_error_btn_retry"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6e6e6"
    android:clickable="true"
    android:gravity="center" >
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:orientation="vertical"
      >
      <imageview
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/wifi"
        android:id="@+id/imageview2" />
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textsize="18dp"
        android:text="数据获取失败"
        ></textview>
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textsize="15dp"
        android:text="请检查网络后,点击重新加载"
        />
    </linearlayout>
  </relativelayout>
</linearlayout>

以上所述是小编给大家介绍的android中替换webview加载网页失败时的页面,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网