在弹出窗口中显示进度条

5
我在弹出框中加载了一个网页视图,并希望在弹出窗口顶部显示进度条,展示网页内容加载的进度。虽然我已经找到了一种显示加载进度条的方法,但是当这个网页视图在弹出框中加载时,进度条并不会在其上方显示。有人能否解释这是为什么呢?
以下是代码:
 @SuppressWarnings("static-access")
    public void showPopUp(String url){
            try{

                Dialog dialog = new Dialog(Links.this);
                LayoutInflater inflater = (LayoutInflater)getSystemService(Links.this.LAYOUT_INFLATER_SERVICE);
                View vi = inflater.inflate(R.layout.link_popup, null);
                dialog.setContentView(vi);
                dialog.setTitle("Title here");
                dialog.setCancelable(true);
                WebView wb = (WebView) vi.findViewById(R.id.WebView01);

                wb.setWebViewClient(new MyWebViewClient());
                wb.getSettings().setJavaScriptEnabled(true);   
                wb.getSettings().setSupportZoom(true);      
                wb.loadUrl(url);
//              final Activity MyActivity = this;  
//              
//              progressDialog = ProgressDialog.show(dialog.getOwnerActivity(), "", "Loading....", true);
//              
//              wb.setWebChromeClient(new WebChromeClient() {
//               public void onProgressChanged(WebView view, int progress)   
//               {
//                   MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
//                   if(progress == 100){
//                    if(progressDialog.isShowing())
//                           progressDialog.dismiss();
//                   }
//                 }
//               }); 

                System.out.println("..loading url..");  
                dialog.show();

             }catch(Exception e){
                 System.out.println("Exception while showing Agreement : " + e.getMessage());
             }
        } 

这个功能没有生效,所以我做了注释。

注:[/code]标签保留。

link_popup.xml

[code]

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill"
android:id="@+id/layout_root"
android:background="#000000"
>

        <ProgressBar

            android:layout_width="fill_parent" 
            android:layout_height="5dip"
            android:layout_alignParentTop="true" 
            style="?android:attr/progressBarStyleHorizontal"
            android:id="@+id/progressbar_Horizontal"
            android:max="100"
            android:background="#228b22"
         />
        <WebView  
            android:id="@+id/WebView01"
            android:layout_below="@id/progressbar_Horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:scrollbars="@null"
          />
</RelativeLayout>

[/code]

6个回答

2
我没有看过你的代码,但是如果你查看xml文件,你会发现有一个RelativeLayout、一个ProgessBar和一个WebView。
当它被加载时,首先会绘制RelativeLayout,然后是Progessbar,最后是WebView。由于你的WebView在宽度和高度上都设置了fill_parent,所以ProgressBar会留在WebView的后面...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="fill"
    android:id="@+id/layout_root"
    android:background="#000000">

    <!-- First the WebView -->
    <WebView  
        android:id="@+id/WebView01"
        android:layout_below="@id/progressbar_Horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:scrollbars="@null"
      />

      <!-- Than the Progessbar on top of it -->
      <ProgressBar
        android:layout_width="fill_parent" 
        android:layout_height="5dip"
        android:layout_alignParentTop="true" 
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/progressbar_Horizontal"
        android:max="100"
        android:background="#228b22"
     />
</RelativeLayout>

不,我想要根据我的代码动态地显示进度条。 - James


0

或者,您可以在清单文件中的活动标记中使用此选项将您的活动显示为对话框:

  android:theme="@android:style/Theme.Dialog"

然后您可以在页面加载时,在您的活动中使用async task来显示进度条。

希望这能帮到您。


0

这个类将显示一个对话框,其中包含一个Web视图,每次加载网页/网站时都会显示旋转器/进度条。

/**
 * @author Sherif
 * 
 * Copyright 2011
 *
 * Dialog that will enable web browsing with a progress dialog.
 *
 */

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class MyLovelyDialog extends Dialog {
    static final float[] DLANDSCAPE = {20, 60};
    static final float[] DPORTRAIt = {40, 60};
    static final FrameLayout.LayoutParams mFrameLayoutParams =
        new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                         ViewGroup.LayoutParams.FILL_PARENT);
    private String mUrl;
    private ProgressDialog mProgressDialog;
    private WebView mWebView;
    private LinearLayout mLinearLayout;

    public MyLovelyDialog(Context context, String url) {
        super(context);
        mUrl = url;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpProgressDialog();
        setUpContent();
        setUpWebView();
        setUpDisplay();
    }
    private void setUpProgressDialog() {
        mProgressDialog = new ProgressDialog(getContext());
        mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mProgressDialog.setMessage("Loading...");
    }
    private void setUpContent() {
        mLinearLayout = new LinearLayout(getContext());
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
    }
    private void setUpDisplay() {
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        final float scale =
            getContext().getResources().getDisplayMetrics().density;
        int orientation =
            getContext().getResources().getConfiguration().orientation;
        float[] dimensions =
            (orientation == Configuration.ORIENTATION_LANDSCAPE)
                    ? DLANDSCAPE : DPORTRAIt;
        addContentView(mLinearLayout, new LinearLayout.LayoutParams(
                display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
                display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
    }
    private void setUpWebView() {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new MyLovelyDialog.MyLovelyWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(mFrameLayoutParams);
        mLinearLayout.addView(mWebView);
    }
    private class MyLovelyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mProgressDialog.show();
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressDialog.dismiss();
        }
    }
}

0

首先,在相对布局中,XML排序中最靠近底部的项目将被显示在顶部。因此,您必须将进度条的XML移到webview下面,这样您才能看到进度条在webview上方。在您的MyWebViewClient中,您应该覆盖onPageFinished,并在该函数内更改进度条的visibilityGONE。您可能希望将进度条的引用传递到MyWebViewClient的构造函数中,以便可以将其用于设置可见性为GONE

示例:

    public class MyWebViewClient extends WebViewClient {

        private ProgressBar pBar;

        public MyWebViewClient(ProgressBar pBar)
        {
            super();
            this.pBar = pBar;
        }

        @Override
        public void onPageFinished (WebView view, String url)
        {
            super.onPageFinished(view, url);
            pBar.setVisibility(View.GONE);
        }

    }

0
在你的Java代码中,进行以下更改:
//              progressDialog = ProgressDialog.show(dialog.getOwnerActivity(), "", "Loading....", true);
//              
//              wb.setWebChromeClient(new WebChromeClient() {
//               public void onProgressChanged(WebView view, int progress)   
//               {
//                   MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
//                   if(progress == 100){
//                    if(progressDialog.isShowing())
//                           progressDialog.dismiss();
//                   }
//                 }
//               }); 

改为:

              progressDialog = (ProgressDialog) vi.findViewById(R.id.progressbar_horizontal);

              wb.setWebChromeClient(new WebChromeClient() {
               public void onProgressChanged(WebView view, int progress)   
               {
                   progressDialog.setProgress(progress * 100); //Make the bar disappear after URL is loaded
                   if(progress == 100){
                    if(progressDialog.isShowing())
                           progressDialog.setVisibility(View.GONE);
                   }
                 }
               });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接