先看一下样式图片(进度条为黄色):

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>