2016-09-29 111 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; 
      } 
     }); 
相关问题