如何轻松地居中进度指示器ProgressDialog(没有标题/文本传承下去)指示器、进度、文本、轻松

2023-09-11 20:13:43 作者:狂傲゛俯视全世界

在调用 progressDialog = ProgressDialog.show(这一点,NULL,NULL,TRUE); 通常开发商想只显示进度指示图像,通常将它期望要在窗口(至少从常规来看UI设计点)内居中。 但形象过于靠左,似乎在右侧一些填充/保证金仍然被计算在右边(可选)文本,虽然我们没有传递任何文本作为参数。 它只是让生活变得更轻松的开发人员:)所以我们并不需要创建只是为了有进度指示器为中心在默认情况下自定义对话框。

When calling progressDialog = ProgressDialog.show(this, null, null, true); usually the developers wants to only show the progress indication image, and usually would it expect to be centered within the window (at least from regular UI design point of view). But the image is too far left, it seems that some padding/margin on the right hand side is still being calculated in for (optional) text on the right, although we're not passing any text as parameter. It would just make life little easier for a developer :) So we don't need to create a custom dialog only in order to have the progress indicator being centered by default.

(我在 HTTP提出这是一个功能要求//$c$c.google.com/p/android/issues/detail?id=9697 ;请明星,如果你也想看看这个改进的)

(I filed this as a feature request at http://code.google.com/p/android/issues/detail?id=9697; please star it if you would also like to see this improved).

现在我的问题:

我怎么能轻易中心的进展的图像,而不必完全创建自己的自定义警告对话框类?任何参数我可能忽略了?

How can I easily center the progress image without having to entirely create my own custom alert dialog class? Any parameter I might have overlooked?

此外,如何将背景设置为透明?

Furthermore, how to set the background to transparent?

我也想知道这个问题: http://stackoverflow.com/questions/2866141/how-to-put-custom-animation-into-a-progressdialog 我还没有真正尝试过自己还没有,但如果你无法创建自定义动画,这意味着如果你想有一个类型的动画进度指示器,你总是需要扩展ProgressDialog类? 纵观ProgressDialog类,但是,我没有发现任何东西比普通的可绘制但其他( ProgressDialog.java ),他们没有使用AnimatedDrawable那里。

I'm also wondering about this: http://stackoverflow.com/questions/2866141/how-to-put-custom-animation-into-a-progressdialog I haven't actually tried it myself yet but if you cannot create custom animations, it means if you want a kind of animated progress indicator, you always need to extend the ProgressDialog class? Looking at the ProgressDialog class though, I don't find anything other than regular drawables though (ProgressDialog.java), they're not using AnimatedDrawable there.

推荐答案

我做了一些测试,我认为实现这一目标的最好办法是做一个自定义的对话框

I did some testing and I feel that the best way to achieve this is doing a custom Dialog.

下面是我做的一个例子。这将回答问题,2号,但会给你如何解决问题的1号的主意。

Here is an example of what I did. This will answer question number 2 but will give you an idea of how to fix question number 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);
    }
}

所有的静态方法来自this链接,没有什么奇怪,但神奇的是发生在构造函数中。 检查我作为参数传递的风格。这种风格是这样的:

All the static methods comes from this link, nothing strange, but the magic occurs in the constructor. Check that I pass as parameter an style. That style is the following:

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

该操作的结果是一个进度旋转在屏幕的中心。如果没有 backgroundDim 和不对话框框。

The result of this is a ProgressBar rotating in the center of the screen. Without backgroundDim and without the Dialog box.