android的WebView进度条
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { Handler handler; WebView wv; TextView textView; ProgressBar progressBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN); LinearLayout rootViewLayout = new LinearLayout(this); rootViewLayout.setOrientation(LinearLayout.VERTICAL); textView = new TextView(this); textView.setTextColor(Color.RED); //生成水平进度条 progressBar = new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal); wv = new WebView(this); rootViewLayout.addView(textView); rootViewLayout.addView(progressBar); rootViewLayout.addView(wv); setContentView(rootViewLayout); wv.getSettings().setAllowFileAccess(true); wv.getSettings().setJavaScriptEnabled(true); //这里吐槽一下这J8网站 写你妹的排队,排你大爷 wv.loadUrl("http://www.12306.cn"); wv.setWebViewClient(new WebViewClient(){ @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); //页面下载完毕,却不代表页面渲染完毕显示出来 //WebChromeClient中progress==100时也是一样 if (wv.getContentHeight() != 0) { //这个时候网页才显示 } } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //自身加载新链接,不做外部跳转 view.loadUrl(url); return true; } }); wv.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub super.onProgressChanged(view, newProgress); //这里将textView换成你的progress来设置进度 // if (newProgress == 0) { // textView.setVisibility(View.VISIBLE); // progressBar.setVisibility(View.VISIBLE); // } textView.setText(newProgress+""); textView.postInvalidate(); progressBar.setProgress(newProgress); progressBar.postInvalidate(); // if (newProgress == 100) { // textView.setVisibility(View.GONE); // progressBar.setVisibility(View.GONE); // } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }