java - Webview 中的进度条

标签 java android webview progress-bar loading

我无法让这个进度条正常工作。任何人都可以帮助我获得来源,我是否缺少什么?

 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 本身工作得很好,我只是想有一个水平加载栏,类似于 dolphin、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);

用于显示进度。您所做的只是显示和隐藏 View

编辑: 添加以下代码。

   @Override
   public void onProgressChanged(WebView view, int progress) {
     progressBar.setProgress(progress);
   }

<罢工> 抱歉,您为此需要一个 webchrome 客户端。所以像下面这样使用。放在下面

web.setWebViewClient(new myWebClient());
//add this below line
web.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
        progressBar.setProgress(progress);
   }
 });

检查 WebView博士,它有一个例子。

关于java - Webview 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605866/

相关文章:

ios - 获取 WKWebView 内某个区域的快照

java - 跨多个 ServerBootstrap 使用相同的线程池是否安全?

java - 日历不按预期工作

android - 删除要从 Android 键盘输入的表情符号

android - Flutter : Could not find a generator for route RouteSettings ("/HomePage", null) 在 _WidgetsAppState

安卓SDK : source code of Contact ContentProvider

google-chrome - 抑制Chrome打包应用程序中的 "Failed to load resource: net::ERR_FILE_NOT_FOUND"

Android webview 不加载页面,但应用程序从浏览器加载

java - 当我可以使用后者创建文件时,为什么我应该创建 File 对象,然后在 FileWriter 或 PrintWriter 中使用它?

java - 添加/累积 "line chart components"