android 加载webview 自定义 失败页面

自定义Android Webview加载失败404页面
本文介绍了如何在Android应用中处理Webview加载404页面时显示自定义视图。通过覆盖WebViewClient的相关方法,隐藏默认的错误页面,并加载预先设计的XML布局,提供更友好的用户体验。

   这是我的第一篇博客,先发一个最近遇到的问题,希望大家多多指教。

   在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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值