如何实现自定义的android WebView错误页
一般来说,可能第一时间想到的是自定义一个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错误页的更多相关文章
- cPanel设置自定义404错误页
利用这个cpanel的错误页工具,你就可以定制错误页面了.设置自定义404错误页,有两种简单的方法. 一,利用cpanel后台控制面板添加设置404自定义错误页的方法 步骤 1.登录cPa ...
- 在Asp.Net的Global.asax中Application_Error跳转到自定义错误页无效的解决办法
在开发Asp.Net系统的时候,我们很多时候希望系统发生错误后能够跳转到一个自定义的错误页面,于是我们经常会在Global.asax中的Application_Error方法中使用Response.R ...
- MVC自定义错误页404静态页
昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...
- ASP.NET MVC下自定义错误页和展示错误页的几种方式
在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...
- ASP.NET MVC-异常处理&自定义错误页
一.应用场景 对于B/S应用程序,在部署到正式环境运行的过程中,很有可能出现一些在前期测试过程中没有发现的一些异常或者错误,或者说只有在特定条件满足时才会发生的一些异常,对于使用ASP.NET MVC ...
- 解决 ASP.NET Core 自定义错误页面对 Middleware 异常无效的问题
我们基于 Razor Class Library 实现了自定义错误页面的公用类库(详见之前的随笔),但是在实际使用时发现如果在 middleware 中发生了异常,则不能显示自定义错误页面,而是返回默 ...
- MVC 实现自定义404错误页
直接进入正题. 在HomeController中有一个NotFound的Action方法. public ActionResult NotFound() { return View(); } 对应的视 ...
- MVC3 自定义的错误页
ASP.NET MVC3中如果配置文件出错了,怎么跳转到自定义的错误页,现在参考网上的档案是说 添加 如下配置文件,并且在路径Views/Shared/下添加Error页面,测试下没有用的,请大家看看 ...
- 关于在 ASP.NET 的 Global.asax 中 Application_Error 方法内,设置跳转到自定义错误页无效的问题
转自:https://www.cnblogs.com/OpenCoder/p/5070645.html 在 Global.asax 中的 Application_Error 方法中,使用 Respon ...
随机推荐
- java 中MAP的按照进入顺序遍历与无序遍历
public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- 在webapp上使用input:file, 指定capture属性调用默许相机,摄像,录音功能
## 在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能 在iOS6下开发webapp,使用inputz之file,很有用 <input type=& ...
- 给定时器settimeout、setInterval调用传递参数
无论是window.setTimeout还是window.setInterval,在使用函数名作为调用句柄时都不能带参数,而在 许多场合必须要带参数,这就需要想方法解决.例如对于函数hello(_na ...
- 空格哥的第一篇Blog
首先十分感谢博客园在这里给我的平台,我在这里学习到了很多东西,响应的,我也想要在这里记录下自己的心路历程!在学习的过程中,希望博客园一直陪伴我,小弟在这里不胜感激!这是小弟的第一篇博客,很多东西都不是 ...
- 2017年1月1日 星期日 --出埃及记 Exodus 21:27
2017年1月1日 星期日 --出埃及记 Exodus 21:27 And if he knocks out the tooth of a manservant or maidservant, he ...
- Mysql修改字段长度
alter table '表名' modify column '列名' varchar(50);
- Radmin Center 1.54 测试版
软件简介:radmin center 用于集中管理安装了 radmin server 的服务器,支持一键远程管理,数据全部本地存储,关键数据使用RC4变形加密.同时保留了radmin的高安全性和高易用 ...
- win7垃圾路径
@echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...
- C# 只移除最后一个字符
string str = "|||"; Console.WriteLine(str.Substring(0, str.Length - 1)); Console.WriteLine ...