如何在WebView Android中加载URL时显示进度条

时间:2015-08-08 09:30:49

标签: java android xml android-studio webview

MainActivity.java
好的,所以这是我加载网址的地方,现在进度条可见,直到页面加载,现在当我点击页面上的链接时,进度条没有出现,我正在搜索10的修复几个小时,但没有一个适合这种条件,进度条应该在新网址加载时开始。 请帮助!

public class MainActivity extends Activity {


private WebView mWebView; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://google.com/");
    mWebView.setWebViewClient(new com.c.MyAppWebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.progressBar1).setVisibility(View.GONE);

            findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
        }});



}


@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}



public void aus (MenuItem menuItem){

    findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);

     mWebView.loadUrl("http://www.google.com");
    mWebView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
        }
    });

}

public void cus (MenuItem menuItem){
    findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);


    mWebView.loadUrl("http://www.google.com");
    mWebView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
        }
    });
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ProgressBar

        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:indeterminate="false"
        android:layout_gravity="center" />

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:3)

如果您只是希望在加载时将进度条显示在Web视图上,则将布局更改为RelativeLayout。将进度条放在底部,以便在视图层次结构的顶部绘制。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:layout_centerInParent="true" />
</RelativeLayout>

您还需要覆盖webview客户端上的方法

mWebView.setWebViewClient(new com.c.MyAppWebViewClient() { 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
    } 

    @Override 
    public void onPageFinished(WebView view, String url) {
        findViewById(R.id.progressBar1).setVisibility(View.GONE);

        findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
    } 
}); 

答案 1 :(得分:1)

webView.setWebViewClient(new WebViewClient() {

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

            @Override
            public void onPageFinished(WebView view, String url) {
                loadingBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }
        });


并将其添加到您的xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:orientation="vertical">
   <WebView
       android:id="@+id/activity_main_webview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:gravity="center"
       android:visibility="gone"
       android:id="@+id/purchase_loadingbar">
       <ProgressBar
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
   </LinearLayout>