如何在android中创建一个半透明的进度对话框

时间:2012-10-25 07:40:12

标签: android

我搜索了很多内容,甚至经历了很多网站问题,但我找不到解决问题的方法。

我想创建一个如下所示的进度对话框。但我不明白该怎么做..我已经尝试过创建一个自定义进度对话框但是......你只能修改内部微调器的外观。 。在我的情况下,我想修改它的布局......我怎样才能实现...... ????

直到现在我已经尝试了以下......

public class nextclass extends Activity {

    Thread t;
    ProgressBar dia;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        MainActivity.progressDialog.dismiss();
        setContentView(R.layout.nextclassview);
    //  requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        // progressDialog = findViewById(R.id.progressBar1);

                        new AuthenticateUserTask().execute();
                    }
                });
    }

    private class AuthenticateUserTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            progressDialog = ProgressDialog.show(
                    nextclass.this, "","Sucessful", true);
/*            progressDialog.setIndeterminate(true);

            progressDialog.setIndeterminateDrawable(getResources()
                    .getDrawable(R.layout.customspinner));
*/


            progressDialog = ProgressDialog.show(nextclass.this, "",
                    "Processing....", true);
            progressDialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

        }

        protected void onPostExecute(String s) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();

            Intent my = new Intent(getApplicationContext(), CountTime.class);
            startActivity(my);

        }

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(2000);![custom spinner][2]
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

有关如何创建自定义对话框的详细信息,请参阅this link

WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();


     WMLP.x = 100;   //x position
     WMLP.y = 100;   //y position

     dialog.getWindow().setAttributes(WMLP);

<强> EDIT2: 使用您的代码进行对话活动示例如下所示

public class DialogActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(" ");
        alertDialog.setMessage("");
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setButton("Accept", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {                
            }
        });
        alertDialog.setButton2("Deny", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertDialog.show();
    }
}

并在android清单文件中

android:theme="@android:style/Theme.Translucent.NoTitleBar"

AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(context, R.style.popup_theme));

在values文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="back_color">#ffffffff</color>
<style name="popup_theme" parent="@android:style/Theme.Translucent.NoTitleBar">
    <item name="android:windowBackground">@color/back_color</item>
    <item name="android:colorBackground">@color/back_color</item>
</style>

答案 1 :(得分:0)

你需要在anim文件夹中有一个旋转动画:

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/linear_interpolator"
>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="51%"
android:repeatCount="infinite"
android:duration="800" />

</set>

在XML中创建一个图像视图:

      <ImageView
       android:id="@+id/loading"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/splash"
       android:layout_centerInParent="true"
       android:layout_gravity="center_horizontal"
       android:layout_marginTop="10dp"
       android:background="@drawable/load_spinner" />

使用以下代码:

       Animation mRotateAnim;
       ImageView mImgVwRotatingImage;
           mImgVwRotatingImage = (ImageView) findViewById(R.id.loading);
       mRotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);


private class splashAsync extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        mImgVwRotatingImage.startAnimation(mRotateAnim);
    }

    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void v) {
        startActivity(new Intent(first.this, second.class));
        finish();
    }
}