为什么进度条适合屏幕宽度?

时间:2013-04-12 17:24:27

标签: java android android-layout

我在下面写了一个方法来显示进度条,但是白色圆框的进度条符合屏幕宽度,我不知道为什么。

public void showProgressBar() {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    ProgressBar pbar = new ProgressBar(activity, null,
            android.R.attr.progressBarStyleLargeInverse);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 50);
    pbar.setLayoutParams(params);
    builder.setView(pbar);
    builder.setCancelable(true);
    progressBar = builder.create();
    progressBar.show();
}

执行上述方法时,进度显示为此图像。

progress bar problem 我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:1)

你应该使用ProgressDialog而不是,它不适合屏幕宽度。

ProgressDialog pd=new ProgressDialog(activity);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();

这可以给你你想要的东西。

这样做:

ProgressDialog pd;//declare as a global variable
public void showProgressBar() {
pd=new ProgressDialog(activity);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();

}

稍后在任何地方取消ProgressDialog

答案 1 :(得分:0)

我用愚蠢的方式解决了它,但它对我很好。

public abstract class AbstractScreen implements IActivity {

protected Response response;
protected FragmentActivity activity;
private CustomProgressDialog progress;

public AbstractScreen(FragmentActivity activity) {
this.activity = activity;
}

public abstract void loadScreen();

@Override
public void preExecution() {
// TODO Auto-generated method stub

}

@Override
public void postExecution(Response response) {
// TODO Auto-generated method stub

}

 public void showProgressBar() {


 progress = new CustomProgressDialog(activity);
 progress.show();
 }

 public void closeProgress() {
 progress.dismiss();
 }

 public FragmentActivity getActivity() {
 return activity;
 }

 private class CustomProgressDialog extends Dialog {

 private Context context;

 public CustomProgressDialog(Context context) {
 super(context);
 this.context = context;
 loadScreen();
 }

 public void loadScreen() {

 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.progress);
 setCancelable(false);

 }

 }
 }

也添加此布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_marginRight="-7dp"
>

<ProgressBar
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 style="?android:attr/progressBarStyleLarge"
 android:id="@+id/progressbar"
 android:layout_gravity="center_horizontal"
 android:max="100"
 />
 </LinearLayout>