使用 WebViewClient 的 ProgressBar 显示进度

Show Progress using ProgressBar for WebViewClient

我正在使用 WebViewClient 加载网页,我想为我的代码显示进度条。我的代码没有逐渐加载进度。它只显示 100% 的进度。如何根据网页加载时间设置进度?

出于我的目的,我只需要在 WebView 上加载“https://www.google.com”。请帮忙。谢谢你。代码如下:

Activity 文件

public class MainActivity extends Activity {

private WebView webView;
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int vc = Color.parseColor("#feca01");


    progressBar = (ProgressBar) findViewById(R.id.determinateBar);
    progressBar.getProgressDrawable().setColorFilter(
            vc, android.graphics.PorterDuff.Mode.SRC_IN);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://deelay.me/5000/http://www.google.com");

}

private class MyBrowser extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon)
    {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
        progressBar.incrementProgressBy(10);

    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.VISIBLE);
       // progressBar.setProgress(0);
    }
 }

}

XML 文件

`

<ProgressBar
    android:id="@+id/determinateBar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="400dp"
    android:padding="0dp"
    android:layout_height="2dp" />

<WebView
    android:layout_height="match_parent"
    android:id="@+id/webView"
    android:layout_weight="0.91"
    android:layout_width="match_parent"
    android:padding="0dp"
    />

`

尝试使用 FinestWebView

使用这个你只需要通过调用加载你的webview -

new FinestWebView.Builder(this).titleDefault("The Finest Artist")
                .show("http://thefinestartist.com");

试试这个我的朋友

WebView webView;
ProgressBar progressBar;

webView = (WebView) findViewById(R.id.web);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);

progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setMax(100);
progressBar.setProgress(1);

webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }
    });
        webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);


        }



        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            progressBar.setVisibility(View.GONE);
        }
    });

Layout

 <WebView
    android:id="@+id/web"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ProgressBar
    android:layout_marginTop="-7dp"
    android:id="@+id/progressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent"
    android:indeterminate="false"
    android:max="100"
    android:progress="1"
    android:theme="@style/ProgressbarTheme" />