1. 程式人生 > >WebView新增進度條

WebView新增進度條

先看一下樣式圖片(進度條為黃色):

1.首先在xml檔案中新增 ProgressBar :

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

2.在程式碼中新增:

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        //顯示進度條
        progressBar.setProgress(newProgress);
        if (newProgress == 100) {
            //載入完畢隱藏進度條
            progressBar.setVisibility(View.GONE);
        }
        super.onProgressChanged(view, newProgress);
    }
});

3.下面是名字為 progress_bar_states 的進度條樣式:

<?xml version="1.0" encoding="utf-8"?><!-- 層疊 -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="2dp" />

            <gradient
                android:angle="270"
                android:centerColor="#E3E3E3"
                android:endColor="#E6E6E6"
                android:startColor="#C8C8C8" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="2dp" />

                <gradient
                    android:centerColor="@color/yellow_Brand"
                    android:endColor="@color/yellow_Brand"
                    android:startColor="@color/yellow_Brand" />
            </shape>
        </clip>
    </item>
</layer-list>

推薦: