62

通常、開発者は電話をかけるときprogressDialog = ProgressDialog.show(this, null, null, true);に進行状況の表示画像のみを表示したいと考えており、通常はウィンドウの中央に配置されることを期待します(少なくとも通常のUIデザインの観点から)。ただし、画像が左にありすぎています。パラメータとしてテキストを渡していないにもかかわらず、右側の一部のパディング/マージンが右側の(オプションの)テキストに対してまだ計算されているようです。開発者の作業が少し楽になるだけです:)したがって、進行状況インジケーターをデフォルトで中央に配置するためだけにカスタムダイアログを作成する必要はありません。

(私はこれを機能リクエストとしてhttp://code.google.com/p/android/issues/detail?id=9697に提出しました。これも改善されたい場合は、スターを付けてください)。

今私の質問:

  1. 独自のカスタムアラートダイアログクラスを完全に作成しなくても、進行状況画像を簡単に中央に配置するにはどうすればよいですか?見落としている可能性のあるパラメータはありますか?

  2. さらに、背景を透明に設定するにはどうすればよいですか?

私もこれについて疑問に思っています: https ://stackoverflow.com/questions/2866141/how-to-put-custom-animation-into-a-progressdialog 私はまだ実際にそれを試していませんが、カスタムを作成できない場合アニメーション、つまり、ある種のアニメーション化された進行状況インジケーターが必要な場合は、常にProgressDialogクラスを拡張する必要がありますか?ProgressDialogクラスを見ると、通常のドローアブル( ProgressDialog.java )以外は見つかりませんが、そこでAnimatedDrawableを使用していません。

4

8 に答える 8

104

私はいくつかのテストを行いましたが、これを達成するための最良の方法はカスタムを行うことだと感じていますDialog.

これが私がしたことの例です。これにより、質問 2 の回答が得られますが、質問 1 の修正方法がわかります。

public class MyProgressDialog extends Dialog {

    public static MyProgressDialog show(Context context, CharSequence title,
            CharSequence message) {
        return show(context, title, message, false);
    }

    public static MyProgressDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate) {
        return show(context, title, message, indeterminate, false, null);
    }

    public static MyProgressDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate, boolean cancelable) {
        return show(context, title, message, indeterminate, cancelable, null);
    }

    public static MyProgressDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate,
            boolean cancelable, OnCancelListener cancelListener) {
        MyProgressDialog dialog = new MyProgressDialog(context);
        dialog.setTitle(title);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        /* The next line will add the ProgressBar to the dialog. */
        dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        dialog.show();

        return dialog;
    }

    public MyProgressDialog(Context context) {
        super(context, R.style.NewDialog);
    }
}

すべての静的メソッドはこのリンクから取得されますが、奇妙なことは何もありませんが、魔法はコンストラクターで発生します。パラメータとしてスタイルを渡すことを確認してください。そのスタイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NewDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:background">@android:color/transparent</item>
    </style>
</resources>

この結果ProgressBar、画面の中央で回転します。箱なしbackgroundDimとなし。Dialog

于 2010-07-12T06:08:26.547 に答える
32

簡単でカスタマイズ可能な方法:

アニメーションの定義: (res/drawable/loader_anim.xml)

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/image_for_rotation"
android:pivotX="50%"
android:pivotY="50%" />

また:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/img_loader_frame1"
android:duration="150"/>
...
<item
android:drawable="@drawable/img_loader_frame2"
android:duration="150"/>
...
</animation-list>

次に、レイアウトを定義します: (res/layout/loader.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal"> 
<ProgressBar
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminateDrawable="@drawable/loader_anim" />
</LinearLayout>

次に、インスタンスの進行状況ダイアログ:

ProgressDialog dialog;
...
dialog = ProgressDialog.show(this,null,null);
dialog.setContentView(R.layout.loader);
...
process();
...
dialog.dismiss();

より詳しい情報:

于 2012-04-02T19:46:04.207 に答える
24

私は以下を使用します。レイアウト ファイルは不要で、画面中央に境界線のないブロッキング プログレス バーを中央に配置します。

private ProgressDialog progressDialog;


setUIToWait(true);

...long process...

setUIToWait(false);


private void setUIToWait(boolean wait) {

    if (wait) {
        progressDialog=ProgressDialog.show(this,null,null);
        progressDialog.setContentView(new ProgressBar(this));
    } else {
        progressDialog.dismiss();
    }

}
于 2013-08-01T07:05:44.230 に答える
19

不確定なプログレスバーのみを表示したい場合。

ProgressDialog progressDialog = ProgressDialog.show(this, null, null, true, false);
progressDialog.setContentView(R.layout.progress_layout);

そして、「progress_layout.xml」という名前のレイアウト xml ファイルを作成します。

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

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>
于 2013-12-20T19:49:13.040 に答える
2
//create Dialog by using below method

@Override
protected Dialog onCreateDialog(int id) {

    switch (id) {

    case DIALOG1_KEY: {
        Dialog dialog = new Dialog(this,R.style.NewDialog);
        dialog.setContentView(R.layout.progress);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}

//then in your onCreate () you can call like below

@Override

public void onCreate(Bundle savedInstatncestate)

{

final Handler mHandler = new Handler();
showDialog(DIALOG1_KEY);
new Thread() {
public void run() {
try {
    sleep(3000);
    Runnable r = new Runnable() 
    {
    public void run() 
    {
               //do your background process

             dismissDialog(DIALOG1_KEY);
    }

        };
mHandler.post(r);
     } catch (Exception e) {
     }
   }
}.start();
}
于 2011-11-22T05:36:56.563 に答える
-1

ProgressBar を使用して LinearLayout に追加すると、私のケースでは次のように機能しました。

ProgressBar mSpinner = new ProgressBar(this); 
mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mSpinner.setBackgroundResource(R.drawable.loading_1);
mSpinner.setIndeterminate(true);

ここに画像の説明を入力

于 2012-08-13T12:55:42.300 に答える