2016-09-29 142 views
0

我有一個web視圖在android中加載一個特定的網站,我想顯示一個加載圖標或進度條點擊webView內的任何鏈接。如何在WebView上顯示加載圖像或進度條

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

    WebSettings webSettings = webViewClient.getSettings(); 

    webSettings.setJavaScriptEnabled(true); 

    webViewClient.setWebViewClient(new WebViewClient()); 

    webViewClient.loadUrl("URL"); 

回答

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()); 
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/ 
0

首先,你要弄清楚當點擊情況:

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

然後,你必須把在FrameLayoutProgressbar你的WebView。

<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; 
      } 
     }); 
相關問題