您的位置:首页 > 移动开发

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 webview 404页面