最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中替換WebView加載網(wǎng)頁失敗時(shí)的頁面

 更新時(shí)間:2017年01月21日 15:54:05   作者:vampire2777  
這篇文章主要介紹了Android中替換WebView加載網(wǎng)頁失敗時(shí)的頁面,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下吧

我們用webView去請求一個(gè)網(wǎng)頁鏈接的時(shí)候,如果請求網(wǎng)頁失敗或無網(wǎng)絡(luò)的情況下,它會返回給我們這樣一個(gè)頁面,如下圖所示:

這里寫圖片描述 

上面這個(gè)頁面就是系統(tǒng)自帶的頁面,你覺得是不是很丑?反正小編本人覺得非常丑,很難看,于是乎小編就在想能不能自定義一個(gè)頁面,當(dāng)數(shù)據(jù)請求失敗時(shí)讓系統(tǒng)來加載我們自定義好的頁面?上網(wǎng)查了很多資料,都沒有關(guān)于這個(gè)問題的解決方法(反正我是沒有找到),經(jīng)過小編的不斷琢磨,今天終于實(shí)現(xiàn)了這個(gè)功能。以下就是本人自定義實(shí)現(xiàn)的數(shù)據(jù)加載失敗時(shí)的頁面:

這里寫圖片描述 

這樣看起來是不是覺得很高大尚。這和我們真正拿到數(shù)據(jù)接口做出來的效果完全一樣。對于用戶來說這樣的體驗(yàn)也是很完美的。

**全部代碼:

一、主代碼:**

MainActivity.Java

package com.example.webview;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
  private WebView webview;
  private WebSettings mWebSettings;
  private View mErrorView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webview = (WebView) findViewById(R.id.main_webview);
    setUpView();
  }
  private void setUpView() {
    //加載需要顯示的網(wǎng)頁
    webview.loadUrl("http://www.baidu.com/");
    //設(shè)置WebView屬性,能夠執(zhí)行Javascript腳本
    webview.getSettings().setJavaScriptEnabled(true);
    mWebSettings = webview.getSettings();
    mWebSettings.setJavaScriptEnabled(true);  //允許加載javascript
    mWebSettings.setSupportZoom(true);     //允許縮放
    mWebSettings.setBuiltInZoomControls(true); //原網(wǎng)頁基礎(chǔ)上縮放
    mWebSettings.setUseWideViewPort(true);   //任意比例縮放
    webview.setWebViewClient(webClient); //設(shè)置Web視圖
  }
  /***
   * 設(shè)置Web視圖的方法
   */
  WebViewClient webClient = new WebViewClient(){//處理網(wǎng)頁加載失敗時(shí)
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
      showErrorPage();//顯示錯誤頁面
    };
  };
  boolean mIsErrorPage;
  protected void showErrorPage() {
    LinearLayout webParentView = (LinearLayout)webview.getParent();
    initErrorPage();//初始化自定義頁面
    while (webParentView.getChildCount() > 1) {
      webParentView.removeViewAt(0);
    }
    @SuppressWarnings("deprecation")
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewPager.LayoutParams.FILL_PARENT, ViewPager.LayoutParams.FILL_PARENT);
    webParentView.addView(mErrorView, 0, lp);
    mIsErrorPage = true;
  }
  /****
   * 把系統(tǒng)自身請求失敗時(shí)的網(wǎng)頁隱藏
   */
  protected void hideErrorPage() {
    LinearLayout webParentView = (LinearLayout)webview.getParent();
    mIsErrorPage = false;
    while (webParentView.getChildCount() > 1) {
      webParentView.removeViewAt(0);
    }
  }
  /***
   * 顯示加載失敗時(shí)自定義的網(wǎng)頁
   */
  protected void initErrorPage() {
    if (mErrorView == null) {
      mErrorView = View.inflate(this, R.layout.activity_error, null);
      RelativeLayout layout = (RelativeLayout)mErrorView.findViewById(R.id.online_error_btn_retry);
      layout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          webview.reload();
        }
      });
      mErrorView.setOnClickListener(null);
    }
  }
}

二、XML布局代碼:

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.webview.MainActivity">
  <WebView
    android:id="@+id/main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </WebView>
</LinearLayout>

2.activity_error.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <RelativeLayout
    android:id="@+id/online_error_btn_retry"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E6E6E6"
    android:clickable="true"
    android:gravity="center" >
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:orientation="vertical"
      >
      <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/wifi"
        android:id="@+id/imageView2" />
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:text="數(shù)據(jù)獲取失敗"
        ></TextView>
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:text="請檢查網(wǎng)絡(luò)后,點(diǎn)擊重新加載"
        />
    </LinearLayout>
  </RelativeLayout>
</LinearLayout>

以上所述是小編給大家介紹的Android中替換WebView加載網(wǎng)頁失敗時(shí)的頁面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

天等县| 右玉县| 常德市| 麟游县| 镇雄县| 邛崃市| 嘉定区| 辽阳县| 阿拉善右旗| 临高县| 新乐市| 墨脱县| 罗平县| 仙桃市| 林甸县| 东乡| 富裕县| 吉林省| 渝中区| 凤凰县| 禹城市| 措美县| 宣恩县| 德州市| 封丘县| 休宁县| 桂东县| 吉隆县| 雅安市| 神池县| 新野县| 石屏县| 吐鲁番市| 德化县| 湘潭市| 年辖:市辖区| 安多县| 扶沟县| 方正县| 黄骅市| 邢台县|