1. 程式人生 > >WebView載入網頁帶進度條

WebView載入網頁帶進度條

圖:


程式碼:

package com.baofoo.mobile.wallet.common.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.baofoo.mobile.view.viewgroup.WalletTitleBar;
import com.baofoo.mobile.wallet.R;
import com.baofoo.mobile.wallet.common.utils.UiUtils;

/**
 * Http://www.xxx.com - 連線公用WebView
 * Created by zst on 2016/3/25.
 */
public class WebViewActivity extends Activity {
    private WalletTitleBar wTitleBar;
    private LinearLayout llBody;
    private WebView wvBody;
    private ProgressBar pbProgress;

    LinearLayout.LayoutParams statusBarHeightParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_web_view_activity);

        wTitleBar = (WalletTitleBar) findViewById(R.id.w_title_bar);
        llBody = (LinearLayout) findViewById(R.id.ll_body);
        wvBody = (WebView) findViewById(R.id.wv_body);
        pbProgress = (ProgressBar) findViewById(R.id.pb_progress);

        wTitleBar.showBackButton();
        wTitleBar.setBackButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        setImmersive();//沉浸式

        init();
    }

    /* 設定沉浸式 */
    private void setImmersive() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明狀態列
        //得到狀態列margin-top高度
        statusBarHeightParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        statusBarHeightParams.setMargins(0, UiUtils.getStatusBarHeight(), 0, 0);
        llBody.setLayoutParams(statusBarHeightParams);
    }

    /* 初始化 */
    private void init() {

        //初始化webview
        wvBody.loadUrl(getIntent().getStringExtra("url"));
        wvBody.getSettings().setJavaScriptEnabled(true);
        wvBody.setWebChromeClient(new WebChromeClient() {//監聽網頁載入
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 網頁載入完成
                    pbProgress.setVisibility(View.GONE);
                } else {
                    // 載入中
                    pbProgress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }
}

佈局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/regest_main_title"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/main_title_bg"
              android:orientation="vertical">

    <LinearLayout
            android:id="@+id/ll_body"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <com.baofoo.mobile.view.viewgroup.WalletTitleBar
                android:id="@+id/w_title_bar"
                android:layout_width="match_parent"
                android:layout_height="47dp"/>

        <ProgressBar
                android:id="@+id/pb_progress"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:indeterminateOnly="false"
                android:max="100"
                android:progressDrawable="@drawable/progress_bar_states" >
        </ProgressBar>

        <WebView
                android:id="@+id/wv_body"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"/>
    </LinearLayout>

</LinearLayout>

drawable:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <gradient
                    android:startColor="#ff0000"
                    android:centerColor="#ffa600"
                    android:endColor="#ff5500"/>
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                        android:startColor="#234"
                        android:centerColor="#234"
                        android:endColor="#a24"/>
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                        android:startColor="#33000001"
                        android:centerColor="#40000000"
                        android:endColor="#44000000"/>
            </shape>
        </clip>
    </item>

</layer-list>