一般来说,可能第一时间想到的是自定义一个html来替代webview内置的异常页面。  但是实际操作时,这种方法比较困难。

这里介绍一个简单的替代方案,希望能有所帮助。

可以采用嵌套layout的方式,然后在webview的错WebViewClient的onReceivedError方法中控制异视图的显示和隐藏,具体代码如下:

public class DefaultWebViewClient extends WebViewClient {
boolean isInErrorState = false;
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//这里可以将异常信息也显示出来
isInErrorState = true;
mEmptyView.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
mEmptyViewIndicator.setImageResource(R.drawable.no_service);
mEmptyView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isInErrorState = false;
webView.reload();
}
});
// super.onReceivedError(view, errorCode, description, failingUrl);
} @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(!isInErrorState) {
mEmptyView.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}else {
mEmptyView.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
}
super.onPageStarted(view, url, favicon);
}
}

对应的layout如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<WebView

android:id="@+id/webView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center_horizontal" />

<LinearLayout

android:orientation="vertical" android:layout_width="match_parent"

android:id="@+id/empty_view_root"

android:layout_height="match_parent"

android:gravity="center"

android:paddingTop="50dp"

android:paddingBottom="50dp">

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/empty_view_indicator"

android:layout_gravity="center"

android:src="@drawable/pic_nothing" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/empty_view_text"

android:textColor="#999999"

android:singleLine="false" />

</LinearLayout>

</RelativeLayout>

如何实现自定义的android WebView错误页的更多相关文章

  1. cPanel设置自定义404错误页

    利用这个cpanel的错误页工具,你就可以定制错误页面了.设置自定义404错误页,有两种简单的方法. 一,利用cpanel后台控制面板添加设置404自定义错误页的方法 步骤       1.登录cPa ...

  2. 在Asp.Net的Global.asax中Application_Error跳转到自定义错误页无效的解决办法

    在开发Asp.Net系统的时候,我们很多时候希望系统发生错误后能够跳转到一个自定义的错误页面,于是我们经常会在Global.asax中的Application_Error方法中使用Response.R ...

  3. MVC自定义错误页404静态页

    昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...

  4. ASP.NET MVC下自定义错误页和展示错误页的几种方式

    在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...

  5. ASP.NET MVC-异常处理&自定义错误页

    一.应用场景 对于B/S应用程序,在部署到正式环境运行的过程中,很有可能出现一些在前期测试过程中没有发现的一些异常或者错误,或者说只有在特定条件满足时才会发生的一些异常,对于使用ASP.NET MVC ...

  6. 解决 ASP.NET Core 自定义错误页面对 Middleware 异常无效的问题

    我们基于 Razor Class Library 实现了自定义错误页面的公用类库(详见之前的随笔),但是在实际使用时发现如果在 middleware 中发生了异常,则不能显示自定义错误页面,而是返回默 ...

  7. MVC 实现自定义404错误页

    直接进入正题. 在HomeController中有一个NotFound的Action方法. public ActionResult NotFound() { return View(); } 对应的视 ...

  8. MVC3 自定义的错误页

    ASP.NET MVC3中如果配置文件出错了,怎么跳转到自定义的错误页,现在参考网上的档案是说 添加 如下配置文件,并且在路径Views/Shared/下添加Error页面,测试下没有用的,请大家看看 ...

  9. 关于在 ASP.NET 的 Global.asax 中 Application_Error 方法内,设置跳转到自定义错误页无效的问题

    转自:https://www.cnblogs.com/OpenCoder/p/5070645.html 在 Global.asax 中的 Application_Error 方法中,使用 Respon ...

随机推荐

  1. arm 2440 linux 应用程序 nes 红白机模拟器 第4篇 linux 手柄驱动支持

    小霸王学习机的真实手柄,实测CPU 占用 80% 接线图: 手柄读时序: joypad.c 驱动: 普通的字符设备驱动. #include <linux/module.h> #includ ...

  2. php常用数组函数回顾一

    数组对于程序开发来说是一个必不可少的工具,我根据网上的常用数组函数,结合个人的使用情况,进行数组系列的总结复习.里面当然不只是数组的基本用法,还有相似函数的不同用法的简单实例,力求用最简单的实例,记住 ...

  3. spring 在静态工具类中使用注解注入bean

    /** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件 ...

  4. 关于sqlserver 2008 无法远程连接的问题

    Sqlserver 2008 无法远程连接,原因无非如下: 1. Sql未配置为允许TCP/IP登录: 2. 防火墙未允许端口1433(或者其他在SQL配置中指定的端口): 3. 命名实例导致的无法连 ...

  5. Swift基础语法学习总结(转)

    Swift基础语法学习总结 1.基础  1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...

  6. ubuntu gtk2.0 一个简单完整的窗口

    //gtk_main();开了线程,关闭窗口并不能退出程序,需要手动添加 //gtk2.0,window quit compelete example #include <gtk/gtk.h&g ...

  7. dsp28377控制DM9000收发数据——第二版程序,能够实现手术功能,但是容易掉帧;使用读取中断寄存器的方式判断中断

    G:\controlSUITE\device_support\F2837xD\v180\F2837xD_examples_Cpu1\emif1_16bit_asram\cpu01\emif1_16bi ...

  8. What is the difference between routine , method , procedure , function ? please explain it with example?

    a method is named and attached to an object. so, for example, a method is like a function but is con ...

  9. 备份MySQL数据库

    备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http:// ...

  10. 微信https请求工具类

    工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...