如何在加载 WebView 之前显示布局的进度条

发布于 2024-12-28 07:27:14 字数 692 浏览 3 评论 0原文

我正在将 WebView 加载到 LinearLayout,并希望在此布局中显示 ProgressBar,直到 WebView 完全加载。我通过以下链接进行访问,它们似乎非常棒,但我不想使用 ProgressDialog,相反,我想在布局内使用进度条。

http://xjaphx.wordpress.com/2011/07 /31/load-webview-with-progressdialog/

如何在 webview 上显示进度条?

我该怎么做?

例如,

if(pos==0)
{
 mLayout.setVisibility(View.VISIBLE);
 mWeb.loadUrl("http://android.stackexchange.com/");
}

mLayout 内,我想显示一个进度条,直到显示页面。

I am loading a WebView to the LinearLayout and want to display a ProgressBar inside this layout until the WebView is fully loaded.I wnet through the below links and they seems to be really great but I do not want to use ProgressDialog, instead I would like to use progressbar inside the layout.

http://xjaphx.wordpress.com/2011/07/31/load-webview-with-progressdialog/

how to show progress bar on webview?

How would I do that?

Say for example,

if(pos==0)
{
 mLayout.setVisibility(View.VISIBLE);
 mWeb.loadUrl("http://android.stackexchange.com/");
}

Inside mLayout I would like to display a progressbar until the page gets displayed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2025-01-04 07:27:14

要在 webview 中定义进度条:

1. 在布局中定义 ProgressBarWebview,如下所示:

<ProgressBar android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="8px"
        android:max="100"
        android:visibility="gone" />

<ScrollView
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fadingEdge="none" >

<WebView
    android:id="@+id/webkit"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</ScrollView>

2. Inside您的活动:

    progressBar = (ProgressBar)findViewById(R.id.progressbar);

    progressBar.setProgress(0);

    progressBar.setVisibility(View.VISIBLE);

    WebView mWebView = (WebView)findViewById(R.id.webkit);

    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    mWebView.setWebChromeClient(new WebChromeClient(){

    public void onProgressChanged(WebView view, int progress) {

         progressBar.setProgress(progress);
         if(progress == 100) {
             progressBar.setVisibility(View.GONE);
          }
       }
    });

To define progress bar inside webview:

1. Define ProgressBar and Webview inside your layout like this:

<ProgressBar android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="8px"
        android:max="100"
        android:visibility="gone" />

<ScrollView
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fadingEdge="none" >

<WebView
    android:id="@+id/webkit"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</ScrollView>

2. Inside your activity:

    progressBar = (ProgressBar)findViewById(R.id.progressbar);

    progressBar.setProgress(0);

    progressBar.setVisibility(View.VISIBLE);

    WebView mWebView = (WebView)findViewById(R.id.webkit);

    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    mWebView.setWebChromeClient(new WebChromeClient(){

    public void onProgressChanged(WebView view, int progress) {

         progressBar.setProgress(progress);
         if(progress == 100) {
             progressBar.setVisibility(View.GONE);
          }
       }
    });
山人契 2025-01-04 07:27:14

您可以像这样简单地使用 webView setPictureListener() :

// here you can start a loading wheel

   webView.setPictureListener(new WebView.PictureListener() {
      public void onNewPicture(WebView webView, Picture picture) {
           // close your loading wheel here
            }
        });

   //load your url
   webView.loadUrl(yourUrl);

You can simply use the webView setPictureListener() like this:

// here you can start a loading wheel

   webView.setPictureListener(new WebView.PictureListener() {
      public void onNewPicture(WebView webView, Picture picture) {
           // close your loading wheel here
            }
        });

   //load your url
   webView.loadUrl(yourUrl);
深爱不及久伴 2025-01-04 07:27:14

我确信这是检测页面加载完成的更好方法。

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            loadingIndicator.setVisibility(View.GONE);
        }
    });

I am sure this is a better way ti detect that a page loading is finished.

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            loadingIndicator.setVisibility(View.GONE);
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文