2011-11-17 48 views
5

Tôi có hộp thoại bật lên trong đó tôi đang tải chế độ xem web. Tôi muốn hiển thị progressBar trong khi webview đang tải trên đầu cửa sổ bật lên. Tôi đã tìm thấy cách hiển thị thanh tiến trình tải trong khi trang web tải nhưng nếu webview này đang tải trong hộp thoại bật lên thì progressBar không được hiển thị trên đầu trang của nó. Ai đó có thể giải thích lý do tại sao điều này xảy ra.Hiển thị tiến trình trong cửa sổ bật lên

Đây là mã

[code]

@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]

Nó không làm việc vì vậy tôi nhận xét nó.

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]

+0

Mã ở đâu? – davidcesarino

Trả lời

0

cách khác, u có thể làm cho hoạt động ur xuất hiện như một hộp thoại sử dụng này trong file manifest: trong thẻ hoạt động

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

và sau đó u có thể hiển thị thanh tiến trình, sử dụng async task trong hoạt động ur trong khi tải trang

hy vọng điều này giúp

0

Lớp này sẽ hiển thị một hộp thoại chứa một webview đó sẽ hiển thị một spinner/tiến độ mỗi khi nó đang tải một trang web/website.

/** 
* @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(); 
     } 
    } 
} 
2

Tôi chưa xem mã của bạn, nhưng nếu bạn xem xml, bạn có RelativeLayout, ProgessBar và WebView.

Khi điều này được tăng cao, trước tiên nó sẽ vẽ RelativeLayout, so với Progessbar của bạn và cuối cùng là WebView của bạn. Kể từ webview của bạn đã fill_parent về chiều rộng và chiều cao, ProgressBar sẽ ở lại phía sau 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> 
+0

Không, tôi muốn hiển thị thanh tiến trình động như tôi đã nhận xét trong mã của mình. – James

0

Vâng đầu tiên ra trong một Layout Relative mục posisioned gần phía dưới theo thứ tự xml sẽ được hiển thị trên hàng đầu. Vì vậy, bạn phải di chuyển xml cho thanh tiến trình của mình bên dưới chế độ xem web để bạn có thể thấy thanh ở trên cùng của chế độ xem web. Trong MyWebViewClient của bạn, bạn nên ghi đè onPageFinished và bên trong hàm đó thay đổi visibility của thanh tiến trình đã biến mất. Bạn có thể muốn chuyển một tham chiếu đến thanh tiến trình vào hàm tạo cho MyWebViewClient để nó có thể được sử dụng để thiết lập khả năng hiển thị biến mất.

Ví dụ:

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

Trong mã java của bạn, làm cho những thay đổi:

//    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(); 
//     } 
//     } 
//    }); 

Thay đổi:

   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); 
        } 
       } 
       }); 
Các vấn đề liên quan