首页 技术 正文
技术 2022年11月20日
0 收藏 317 点赞 3,712 浏览 1965 个字

一般来说,可能第一时间想到的是自定义一个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>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,202
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,662
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,500
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,270
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,916
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,072