Android中替换WebView加载网页失败时的页面

  

我们用webView去请求一个网页链接的时候,如果请求网页失败或无网络的情况下,它会返回给我们这样一个页面,如下图所示:

  

 Android中替换WebView加载网页失败时的页面”>,</p>
  <p>上面这个页面就是系统自带的页面,你觉得是不是很丑吗?反正小编本人觉得非常丑,很难看,于是乎小编就在想能不能自定义一个页面,当数据请求失败时让系统来加载我们自定义好的页面?上网查了很多资料,都没有关于这个问题的解决方法(反正我是没有找到),经过小编的不断琢磨,今天终于实现了这个功能。以下就是本人自定义实现的数据加载失败时的页面:<br/>
  </p>
  <p> <img src=   包com.example.webview;   进口android.support.v4.view.ViewPager;   进口android.support.v7.app.AppCompatActivity;   进口android.os.Bundle;   进口android.view.View;   进口android.webkit.WebSettings;   进口android.webkit.WebView;   进口android.webkit.WebViewClient;   进口android.widget.LinearLayout;   进口android.widget.RelativeLayout;   公开课MainActivity延伸AppCompatActivity {   私人WebView WebView;   私人WebSettings mWebSettings;   私人观点mErrorView;   @Override   保护空白>   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools " android: id=癅 + id/activity_main”   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:取向=按怪薄?   工具:上下文=癱om.example.webview.MainActivity”比;   & lt; WebView   android: id=癅 + id/main_webview”   android: layout_width=" match_parent "   android: layout_height=癿atch_parent”比;   & lt;/WebView>   & lt;/LinearLayout>      

2. activity_error.xml         & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android:取向=按怪薄?   android: layout_width=" match_parent "   android: layout_height=癿atch_parent”比;   & lt;使用   android: id=癅 + id/online_error_btn_retry”   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:背景=" # E6E6E6”   android:点击=" true "   android:重力="中心"的在   LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:重力="中心"   android:取向=按怪薄?   比;   & lt; ImageView   android: layout_width=" 70 dp”   android: layout_height=" 70 dp”   android: src=" https://www.yisu.com/zixun/@drawable/wifi”   android: id=癅 + id/imageView2”/比;   & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: textSize=" 18 dp "   android:文本="数据获取失败”   祝辞& lt;/TextView>   & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: textSize=" 15 dp "   android:文本="请检查网络后,点击重新加载”/比;   & lt;/LinearLayout>   & lt;/RelativeLayout>   & lt;/LinearLayout>      

以上所述是小编给大家介绍的Android中替换WebView加载网页失败时的页面,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

Android中替换WebView加载网页失败时的页面