0

我到处找了,一无所获!我需要将我的progressDialog 的位置设置在水平和垂直中心。但是当我通过命令行编译我的代码(我不使用 eclipse)时,会显示一条错误消息:“找不到符号 > Gravity ..”。我认为这是包含问题,我已经导入了无用文件的战利品,因为我不知道要导入什么。有人可以帮助我吗?这是代码:

package com.worfut.project;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.view.KeyEvent; 
import android.view.inputmethod.EditorInfo; 
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.ContextThemeWrapper;
import android.widget.ShareActionProvider;
import android.widget.LinearLayout;
import android.view.Window;

public class MainActivity extends Activity {


WebView WorfutWeb;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);
                 final Activity activity = this;

    WorfutWeb = (WebView) findViewById(R.id.mainWebView);

    WorfutWeb.getSettings().setSupportZoom(false);
    WorfutWeb.getSettings().setBuiltInZoomControls(false);
    WorfutWeb.setBackgroundColor(Color.BLACK);

    WorfutWeb.setWebViewClient(new myWebClient());
    WorfutWeb.getSettings().setJavaScriptEnabled(true);
        // prepare for a progress bar dialog
        progressBar = new ProgressDialog(this, R.id.mainWebView);
        progressBar.setCancelable(true);
        progressBar.setIndeterminate(false);
        progressBar.getWindow().setGravity(Gravity.CENTER);
        progressBar.setMessage("Caricamento in corso..");
        progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();                                                

        WorfutWeb.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, final int progress)
            {
                activity.setProgress(progress * 100);
                progressBar.setProgress(progress);

                if(progress == 100)
                {
                    progressBar.dismiss();
                }

            }
        });
    WorfutWeb.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
   String html1 = "<html><body><br>";
   String html2 = "<div style='background: #007ac1; border: 2px solid lightblue; border-radius: 8px; padding: 4px;'>";
   String html3 = "<p style='font-weight: bolder;color: white;'><h2><u>Si &egrave; verificato un errore!</u></h3><br>";
   String html4 = "Possibili cause:<br><ul><li>";
   String html5 = "Non si dispone di una connessione Internet;</li><ul><li>Chiudere l'applicazione</li><li>Attivare la connessione Internet</li><li>Riavviare l'applicazione</li></ul><li>L'applicazione non risponde<ul><li>Chiudere e riaprire l'applicazione</li><li>Scriveteci sul Market Android</li></ul>";
   String html6 = "</li></ul></p><br><p align=right><font color=white><em>Worfut Staff.</em>&nbsp;</font></p></div></body></html>";
   String mime = "text/html";
   String encoding = "utf-8";
   view.loadData(html1+html2+html3+html4+html5+html6, mime, encoding);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });
    Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
    WorfutWeb.startAnimation(fadeInAnimation);
    WorfutWeb.loadUrl("http://www.americantrip.altervista.org/try");


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.menu_about:
WorfutWeb.loadUrl("http://americantrip.altervista.org/try/info.php"); 
// inside the menu button you don't need to call Super.loadUrl, LoadUrl its enough 
        return true;
    case R.id.menu_help:
WorfutWeb.loadUrl("http://americantrip.altervista.org/try/help.php"); 
// inside the menu button you don't need to call Super.loadUrl, LoadUrl its enough 
        return true;
    case R.id.menu_search:
WorfutWeb.loadUrl("http://americantrip.altervista.org/try/cerca.php"); 
        return true;
    case R.id.menu_exit:
       finish();
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void onResume() {  
   super.onResume();  
   if (WorfutWeb != null) {  
    WorfutWeb.resumeTimers();  
   }  
 }  

public void onPause() {  
   super.onPause();  
   if (WorfutWeb != null) {  
    WorfutWeb.pauseTimers();  
   }  
 }  


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

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;

    }
    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.dismiss();
    }
}

 // To handle "Back" kewy press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && WorfutWeb.canGoBack()) {
        WorfutWeb.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

这是布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/sfondo" 
    android:gravity="center"> 
    > 
    <WebView android:id="@+id/mainWebView" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" /> 
 </LinearLayout>

有错误的部分是这样的:

    progressBar.getWindow().setGravity(Gravity.CENTER);

我认为它是正确的,但也许我必须导入其他东西。谢谢你们。

PS 我已经看过http://developer.android.com/reference/android/widget/LinearLayout.html#setGravity(int) 并且我已经从那里获取了“import android.widget.LinearLayout”。好像是一样的,别跑。

4

2 回答 2

1

尝试:

import android.view.Gravity;
于 2013-02-23T10:26:03.540 回答
0
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/sfondo" 
    > 
    > 
    <WebView android:id="@+id/mainWebView" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
         /> 
 </LinearLayout>

修改了您的布局,我猜当您在中心声明布局时,进度不会在中心。无论如何,如果那对您没有帮助。最简单的方法是。

                    FrameLayout.LayoutParams para=new FrameLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
                para.gravity = Gravity.CENTER;
                progressBar.setLayoutParams(para);

它应该可以工作。如果这解决了您的问题,请接受答案,以便其他人可以使用它。干杯

于 2013-02-23T11:17:31.207 回答