居中自定义进度对话框

时间:2013-06-19 07:21:42

标签: android progressdialog

我创建了一个Custom ProgressDialog,如下所示: 首先,我有一个动画列表,包含9个连续的Imges

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@drawable/icon_progress_dialog_drawable_1" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_2" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_3" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_4" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_5" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_6" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_7" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_8" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_9" android:duration="150" />

然后我有自定义样式

 <style name="CustomDialog" parent="@android:style/Theme.Dialog" >
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>      
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:gravity">center</item>
    <item name="android:minHeight">70dip</item>
    <item name="android:maxHeight">80dip</item>       

现在在代码中我将其称为如下

 dialog = new ProgressDialog(KaguaHome.this); 
        dialog.setProgressStyle(R.style.CustomDialog);
        dialog.setIndeterminate(true);
        dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_dialog_icon_drawable_animation));
        dialog.show();

结果是 enter image description here

我想要只用图像(没有白色背景)来实现进度对话框,它应该居中,我应该修改什么?

2 个答案:

答案 0 :(得分:5)

试试这个并说出它是如何工作的。我试过这个,对我来说它就是中心。

<style name="NewDialog" parent="@android:style/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">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item ame="android:indeterminateDrawable">@anim/progress_dialog_icon_drawable_animation</item>
    </style>

并将其声明为

customDialog = new Dialog(getActivity(),R.style.NewDialog);

看看是否有效。

答案 1 :(得分:1)

    Dialog dialog = new Dialog(FlashActivity.this, android.R.style.Theme_Dialog);
    dialog.setCancelable(false);
    ImageView imageView = new ImageView(this);
    imageView.setScaleType(ScaleType.FIT_XY);       
    imageView.setBackgroundResource(R.drawable.progress_dialog_icon_drawable_animation);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(imageView);       
    dialog.setCanceledOnTouchOutside(false);
    dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.color_drawable_1));

    dialog.show();
    AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
    animationDrawable.start();

以下是color_drawable_1

   <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@color/transparent"></item>

    </selector>  

下面在colors.xml中是透明的(你可以在这里添加任何透明色)

   <?xml version="1.0" encoding="utf-8"?>
   <resources>

      <color name="transparent">#00ffffff</color>

   </resources>

以下是您的progress_dialog_icon_drawable_animation

 <?xml version="1.0" encoding="utf-8"?>
 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="false">

<item android:drawable="@drawable/icon_progress_dialog_drawable_1" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_2" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_3" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_4" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_5" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_6" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_7" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_8" android:duration="150" />
<item android:drawable="@drawable/icon_progress_dialog_drawable_9" android:duration="150" />

</animation-list>

由于您正在创建自定义对话框,因此不要将对话框创建为progressdialog的实例。创建 使用对话框主题的Dialog实例。将图像视图添加到其视图内容。将drawable设置为图像视图并启动动画。现在您必须根据需要修改Windows对话框。将它设为无标题,因为对话框主题通常带有标题,您必须使用任何透明颜色设置其背景颜色

尝试以上操作,您可以告诉我任何问题。它有效。