android 加载webview 自定义 失败页面
2016-01-13 17:03
676 查看
这是我的第一篇博客,先发一个最近遇到的问题,希望大家多多指教。
在android 原生应用了加载H5页面遇到404页面的问题,默认的404页面在手机显示出来很丑的,所以自定义页面很有必要的。
关键是隐藏默认的404页面 ,然后加上一层自定义的view.
上部分代码:
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//TODO
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//TODO
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
/*隐藏默认的404页面*/
view.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
/**加载自定义页面*/
loadFailure();
}
});
xml布局 webview加载失败的时候显示 load_ll (自定义布局):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_top"
android:orientation="vertical" >
<WebView
android:id="@+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/load_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/ivory"
android:layout_marginTop="-100dp"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="@+id/load_failure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/load_failure"
android:layout_gravity="center_horizontal"
android:contentDescription="@null" />
<TextView
android:id="@+id/load_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/load_failure"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:textSize="16sp" />
<Button
android:id="@+id/load_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="@string/load_retry" />
</LinearLayout>
</FrameLayout>
在android 原生应用了加载H5页面遇到404页面的问题,默认的404页面在手机显示出来很丑的,所以自定义页面很有必要的。
关键是隐藏默认的404页面 ,然后加上一层自定义的view.
上部分代码:
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//TODO
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//TODO
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
/*隐藏默认的404页面*/
view.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
/**加载自定义页面*/
loadFailure();
}
});
xml布局 webview加载失败的时候显示 load_ll (自定义布局):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_top"
android:orientation="vertical" >
<WebView
android:id="@+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/load_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/ivory"
android:layout_marginTop="-100dp"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="@+id/load_failure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/load_failure"
android:layout_gravity="center_horizontal"
android:contentDescription="@null" />
<TextView
android:id="@+id/load_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/load_failure"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:textSize="16sp" />
<Button
android:id="@+id/load_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="@string/load_retry" />
</LinearLayout>
</FrameLayout>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories