Java-Webview中的进度栏
作者:互联网
我无法使此进度栏正常工作.有人可以帮助我提供消息来源吗?我缺少什么吗?
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.setWebViewClient(new myWebClient());
//ADDING THIS RESOLVED THE PROGRESS BAR ACTUALLY LOADING.
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress); }
});
//ADDING THIS RESOLVED THE PROGRESS BAR ACTUALLY LOADING.
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(true);
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.loadUrl("http://www.google.com");
然后在代码后面
//Loading the Actual Webpage
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);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
progressBar.setVisibility(View.VISIBLE);
return true;
}
//web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
Webview本身工作得很好,我只是想拥有一个水平加载栏,类似于海豚,Chrome等浏览器.
这是布局.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgapp"
android:fadingEdge="horizontal"
android:gravity="center"
android:orientation="vertical" >
<include
android:id="@+id/inc_blue"
android:layout_width="fill_parent"
android:layout_height="50dp"
layout="@layout/inc_enphase_bar_with_title_buttons" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minHeight="8dip" />
<WebView
android:id="@+id/webview01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.15"
android:fadingEdge="vertical" >
</WebView>
我没有遇到任何错误或烦恼,因此没有logcat,它只是实际加载,它在加载网页时显示进度条,但没有动画或加载动静.
解决方法:
您还应该使用以下命令设置中间进度
progressBar.setProgress(int);
显示进度.您要做的就是显示和隐藏视图
编辑:
添加以下代码.
@Override
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
抱歉,您需要一个webchromeclient.因此,使用如下所示.放在下面
web.setWebViewClient(new myWebClient());
//add this below line
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
检查WebView Doc,它有一个示例.
标签:webview,progress-bar,loading,java,android 来源: https://codeday.me/bug/20191127/2075367.html