如何在WebView上显示加载图像或进度条

时间:2016-09-29 17:05:31

标签: java android webview

我在Android中有一个加载特定网站的webView,我想在点击webView中的任何链接时显示加载图标或进度条。

    webViewClient = (WebView) findViewById(R.id.contentContainer);

    WebSettings webSettings = webViewClient.getSettings();

    webSettings.setJavaScriptEnabled(true);

    webViewClient.setWebViewClient(new WebViewClient());

    webViewClient.loadUrl("URL");

3 个答案:

答案 0 :(得分:1)

 public class CustomWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            showProgressBar();
        }

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


    webViewClient.setWebViewClient(new CustomWebViewClient());

答案 1 :(得分:0)

 webViewClient = (WebView) findViewById(R.id.contentContainer);

    WebSettings webSettings = webViewClient.getSettings();

    webSettings.setJavaScriptEnabled(true);

    webViewClient.setWebViewClient(new WebViewClient(){

     public void onProgressChanged(WebView view, int progress) {
             activity.setTitle("Loading...");
             activity.setProgress(progress * 100);
                if(progress == 100)
                   activity.setTitle("Your Title");
             });

    webViewClient.loadUrl("URL");

Following Link May help you as well : http://www.firstdroid.com/2010/08/04/adding-progress-bar-on-webview-android-tutorials/

答案 2 :(得分:0)

首先,您必须弄清楚点击发生的时间:

webView.setWebViewClient(new WebViewClient() { 
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                webView.loadUrl(url); 
                // Here the String url hold 'Clicked URL' 
                return false; 
            } 
        });

然后,您必须使用WebView将Progressbar放入FrameLayout

<FrameLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">

<Progressbar
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout> 

因此,当点击发生时,您可以在活动中显示进度条。

webView.setWebViewClient(new WebViewClient() { 
            public boolean shouldOverrideUrlLoading(WebView view, String url){
            if (url.equals("your_url"){
            progressbar.setVisibility(View.VISIBLE);
            } 
                return false; 
            } 
        });