Centering Custom Progress Dialog

2020-02-02 00:00发布

问题:

I have created a Custom ProgressDialog as follows: First I have an Animation List with 9 sequential 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" />

Then I have a custom style

 <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>       

Now In code I call it as follows

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

The result is

I however want to achieve a progress Dialog with only the image (No white background) and It should be centered,what should I modify?

回答1:

Try this and say how this works. I have tried this and for me it comes at center.

<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>

And declare this as

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

See if this works.



回答2:

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

The below is 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>  

The below is transparent in colors.xml(you can put any transparent color here)

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

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

   </resources>

The below is your 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>

Since you are creating custom dialog do not create dialog as an instance of progressdialog. Create an instance of Dialog using dialog theme. Add image view to its view content. Set your drawable to image view and start the animation. Now you have to modify the windows dialog as you required. Make it as no title because dialog theme generally comes with title and you have to set its background color with any transparent color

Try the above and you can let me know any issues. It works.