欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

這里寫(xiě)圖片描述 

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

這里寫(xiě)圖片描述 

這樣看起來(lái)是不是覺(jué)得很高大尚。這和我們真正拿到數(shù)據(jù)接口做出來(lái)的效果完全一樣。對(duì)于用戶來(lái)說(shuō)這樣的體驗(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)頁(yè)
    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)頁(yè)基礎(chǔ)上縮放
    mWebSettings.setUseWideViewPort(true);   //任意比例縮放
    webview.setWebViewClient(webClient); //設(shè)置Web視圖
  }
  /***
   * 設(shè)置Web視圖的方法
   */
  WebViewClient webClient = new WebViewClient(){//處理網(wǎng)頁(yè)加載失敗時(shí)
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
      showErrorPage();//顯示錯(cuò)誤頁(yè)面
    };
  };
  boolean mIsErrorPage;
  protected void showErrorPage() {
    LinearLayout webParentView = (LinearLayout)webview.getParent();
    initErrorPage();//初始化自定義頁(yè)面
    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)自身請(qǐng)求失敗時(shí)的網(wǎng)頁(yè)隱藏
   */
  protected void hideErrorPage() {
    LinearLayout webParentView = (LinearLayout)webview.getParent();
    mIsErrorPage = false;
    while (webParentView.getChildCount() > 1) {
      webParentView.removeViewAt(0);
    }
  }
  /***
   * 顯示加載失敗時(shí)自定義的網(wǎng)頁(yè)
   */
  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="請(qǐng)檢查網(wǎng)絡(luò)后,點(diǎn)擊重新加載"
        />
    </LinearLayout>
  </RelativeLayout>
</LinearLayout>

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

相關(guān)文章

  • 非常好看的android音量旋鈕

    非常好看的android音量旋鈕

    這篇文章主要為大家詳細(xì)介紹了android好看的音量旋鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Android自定義View實(shí)現(xiàn)五子棋小游戲

    Android自定義View實(shí)現(xiàn)五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android發(fā)送短信方法總結(jié)

    Android發(fā)送短信方法總結(jié)

    這篇文章主要介紹了Android發(fā)送短信方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android發(fā)送短信的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-11-11
  • Android打開(kāi)GPS導(dǎo)航并獲取位置信息返回null解決方案

    Android打開(kāi)GPS導(dǎo)航并獲取位置信息返回null解決方案

    最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來(lái)與大家分享
    2013-01-01
  • loadavg數(shù)據(jù)異常引發(fā)問(wèn)題起源分析

    loadavg數(shù)據(jù)異常引發(fā)問(wèn)題起源分析

    這篇文章主要為大家介紹了loadavg數(shù)據(jù)異常引發(fā)問(wèn)題起源分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例

    Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例

    這篇文章主要介紹了Android kotlin使用注解實(shí)現(xiàn)防按鈕連點(diǎn)功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼

    android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼

    這篇文章主要介紹了android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Android開(kāi)發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法匯總

    Android開(kāi)發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法匯總

    這篇文章主要介紹了Android開(kāi)發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法,下面小編把每種方法通過(guò)實(shí)例逐一給大家介紹,需要的朋友可以參考下
    2016-09-09
  • AndroidView與Compose框架交互實(shí)現(xiàn)介紹

    AndroidView與Compose框架交互實(shí)現(xiàn)介紹

    Android Compose自推出正式版本后,google 就一直推薦使用Compose來(lái)開(kāi)發(fā)。正好疫情期間,作為一個(gè) Android 摸魚(yú)達(dá)人,就來(lái)摸索一下Compose的開(kāi)發(fā)。說(shuō)實(shí)話開(kāi)發(fā)了2天感覺(jué)對(duì)Android 開(kāi)發(fā)人員來(lái)說(shuō)變化是巨大的,但是作為從業(yè)者我們還必須學(xué)習(xí)和學(xué)會(huì),才能不被甩開(kāi)
    2022-09-09
  • Android使用AlertDialog創(chuàng)建對(duì)話框

    Android使用AlertDialog創(chuàng)建對(duì)話框

    這篇文章主要為大家詳細(xì)介紹了Android使用AlertDialog創(chuàng)建對(duì)話框的方法料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12

最新評(píng)論