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>

推荐:

WebView 复制粘贴文本

WebView加载页面错误之后的自定义显示错误界面

WebView点击返回键回到上一个html

WebView 获取标题

更多内容戳我&

猜你喜欢

转载自blog.csdn.net/wuqingsen1/article/details/82622010