如何在tab2 webView中添加进度条

时间:2015-10-30 13:07:12

标签: android webview

我想在webView中添加进度,但是当我尝试按照thisthis添加进度条时,但它太令人困惑了我无法理解我的态度一个新手。

我想在我的tab2活动中添加进度条

package com.freerechargeapp.weebly.free_recharge_app;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;


public class tab2 extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.tab2, container, false);





    WebView webView = (WebView) view.findViewById(R.id.webview1);
    webView.loadUrl("http://google.com");



    return view;



}

}

这是我的tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

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



    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"


 />

    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/progressBar1"/>




</RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

试试这种方式,

public class tab2 extends Fragment {

WebView webView ;
 ProgressBar progressBar;

  @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab2, container, false);



        webView = (WebView) view.findViewById(R.id.webview1);
        progressBar = (ProgressBar)view.findViewById(R.id.progressBar1);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://google.com");

        return view;
  }

public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
                progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }


}