【问题标题】:Custom ProgressDialog extends Dialog or ProgressDialog?自定义 ProgressDialog 扩展 Dialog 或 ProgressDialog?
【发布时间】:2012-10-12 16:56:25
【问题描述】:

我只是想创建一个旋转的进度对话框。没有文字,没有边框,没有背景。只是一个旋转的通知,轻轻地位于内容顶部的屏幕中心。

我在创建此自定义对话框时看到了两个不同的 Stack Overflow 问题。

他们都使用这种样式:

   <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:colorBackground">#ffffff</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:width">600dip</item>
    <item name="android:height">100dip</item>
     <item name="android:textColor">#FF0000</item>

</style>

但是在Java方面,一个扩展Dialog并包含大量自定义内容,另一个就是这样:

public class MyProgressDialog extends ProgressDialog {

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

    }

}

在第一个(扩展对话框)上,我得到一个空白。没有对话。没有。在上面的代码示例中,我看到一个缩小的小对话框在一个偏离中心的黑框内旋转。

哪种方法更正确,我可以提供如何实现的示例吗?

另外,如何使该对话框透明/无边框?

【问题讨论】:

    标签: android progressdialog


    【解决方案1】:

    现在执行此操作的“正确方法”是扩展 DialogFragment http://developer.android.com/reference/android/app/DialogFragment.html

    如果您不使用兼容性库,那么您就不是在当前的 Android 世界中进行编程。

    如果你想扩展 Dialog,那么我这里有一些示例代码:https://github.com/tom-dignan/nifty/tree/master/src/com/tomdignan/nifty/dialogs 在这段代码中,我通过扩展 Dialog 实现了一种“进度对话框”。我扩展 Dialog 是因为我想要完全控制并且希望我的 Dialog 是全屏的。

    不过,我建议阅读上面的 DialogFragment 文档并使用它。

    所以说真的,这里没有对错之分。扩展这三个类中的任何一个都可以,但最简洁和最新的方法是 DialogFragment 方法。

    【讨论】:

    • 我实际上正在使用这个库。我会试一试并报告。谢谢。
    • 另外,扩展 DialogFragment 是否有助于我创建的自定义外观?透明/无边框?
    • 您可以完全控制样式和主题:DialogFragment.STYLE_NO_TITLE、DialogFragment.STYLE_NO_FRAME 等。您可以完全控制在 onCreateView 中创建的视图。提示:如果为了便携性而使某些东西“透明”,请使用#00000100
    【解决方案2】:

    一种方法是使用自定义对话框,请求功能 No_Title 并将背景可绘制资源设置为透明。

    您可以使用以下代码,我只是在 Android 4.0.3 ICS 上编写并测试了它。

    布局文件夹中的 xml 布局是,比如 dialog_progress.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/dialogProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>
    
    </LinearLayout>
    

    显示对话框并使其透明的java代码如下。

    Dialog dialog = new Dialog (this);
    
    dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
    dialog.setContentView (R.layout.dialog_progress);
    dialog.getWindow ().setBackgroundDrawableResource (android.R.color.transparent);
    dialog.show ();
    

    【讨论】:

      【解决方案3】:

      通过在 ImageView 上使用逐帧动画而不是制作自定义进度对话框,可以很容易地实现类似的效果。为此,您只需要一系列图像(可以从任何 gif 中提取)。然后您可以在其上应用逐帧动画。它会做你想做的事。

      我用下面的代码做了类似的事情——:

      //逐帧动画的XML

      <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
      android:oneshot="false" >
      
      <item
          android:drawable="@drawable/frame00"
          android:duration="150"/>
      <item
          android:drawable="@drawable/frame01"
          android:duration="150"/>
      <item
          android:drawable="@drawable/frame02"
          android:duration="150"/>
      <item
          android:drawable="@drawable/frame03"
          android:duration="150"/>
      <item
          android:drawable="@drawable/frame04"
          android:duration="150"/>
      <item
          android:drawable="@drawable/frame05"
          android:duration="150"/>
      <item
          android:drawable="@drawable/frame06"
          android:duration="150"/>
      

      开始动画的Java代码:-

      ImageView iv = (ImageView)findViewById(R.id.progressDialog);
      iv.setBackgroundResource(R.anim.progress_dialog_icon_drawable_animation);
      AnimationDrawable aniFrame = (AnimationDrawable) iv.getBackground();
      aniFrame = (AnimationDrawable) iv.getBackground();
      aniFrame.start();
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-25
        • 2011-02-18
        • 2012-12-04
        • 1970-01-01
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 2010-12-30
        相关资源
        最近更新 更多