我开发了一个应用程序,它有一个Web视图和一个进度条,所以我想在加载网页的进度条中添加显示进度等功能,以便它实现我有我放在这里的代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.web);
progressBar = (ProgressBar) findViewById(R.id.progrss);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.technotalkative.com");
}
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
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);
}
}
}
layout file..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:id="@+id/progrss"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/blue_progressbar"
/>
<WebView
android:id="@+id/web"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
</WebView>
</LinearLayout>
答案 0 :(得分:5)
准备好您的progress bar
progressBar.setProgress(0);
probressbar.setMax(100);
您应该使用
设置intermediate progress
progressBar.setProgress(int);
并按照以下方式实施WebViewClient
:
web.setWebViewClient(new myWebClient());
//add this below line
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progresss == 100) {
progressBar.setVisibility(View.GONE);
} else{
progressBar.setVisibility(View.VISIBLE);
}
}
});
答案 1 :(得分:2)
在ProgressBar的xml文件中设置进度。
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:id="@+id/progrss"
android:progress="100"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/blue_progressbar"
/>
并在myWebClient方法中实现它
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
答案 2 :(得分:1)
使用以下课程。它会在加载时显示进度条 网页。
public class WebSearchActivity extends Activity{
private WebView webView;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.websearch);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setInitialScale(60);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("https://www.google.co.in");
webView.setWebViewClient(new loadWebPage());
pd = new ProgressDialog(this);
pd.setMessage("Loading WebPage");
pd.show();
pd.setCanceledOnTouchOutside(false);
}
/*
* This Class is used to get the WebView loading info
*/
private class loadWebPage extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(final WebView view,
final String url) {
// load the given url
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if (pd.isShowing()) {
// dismiss the progress dialog
pd.dismiss();
}
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
}
}