【问题标题】:How to make Custom Dialog look like Alert Dialog?如何使自定义对话框看起来像警报对话框?
【发布时间】:2011-03-27 20:54:48
【问题描述】:

我创建了自己的(自定义)对话框。但希望有它的风格像原来的Alert Dialog。 IE。底部有深色标题背景和灰色按钮背景。 有没有相同的现成xml? (所以,我不会担心确切的颜色、高度、字体大小等)

【问题讨论】:

    标签: android android-alertdialog customdialog


    【解决方案1】:

    这个答案不正确

    使用Theme.Dialog.Alert

    来自themes.xml

    <!-- Default theme for alert dialog windows, which is used by the
            {@link android.app.AlertDialog} class.  This is basically a dialog
             but sets the background to empty so it can do two-tone backgrounds. -->
    <style name="Theme.Dialog.Alert" parent="@android:style/Theme.Dialog">
        <item name="windowBackground">@android:color/transparent</item>
        <item name="windowTitleStyle">@android:style/DialogWindowTitle</item>
        <item name="windowIsFloating">true</item>
        <item name="windowContentOverlay">@null</item>
    </style>
    

    这可以在 XML 布局或 Android 清单中应用,如 referenced here

    <activity android:theme="@android:style/Theme.Dialog.Alert">
    

    或使用setTheme(int) 进入活动。但是,这似乎不是推荐的做法。此bug report 中显示的简单示例代码。

    【讨论】:

    • 谢谢,@Karl。如何将它应用到我自己的对话框中?
    • 这取决于您如何定义您的对话框 - 通过在 XML 中或以编程方式。似乎不建议以编程方式应用主题。我已经稍微更新了我的答案。另请参阅此链接:anddev.org/applying_a_theme_to_your_application-t817.html
    • 我的回答不正确。测试,我看到 Theme.Dialog.Alert 甚至没有公开。它仅被 AlertDialog 类用作简单模板。真正的样式稍后在类中进行硬编码。对不起。
    • 那我还能尝试什么?如何阅读这些标准颜色/字段高度以将它们应用到我的对话框中?
    • 您必须自己设置对话框的样式。在themes.xml 中是&lt;style name="Theme.Dialog"&gt; 部分,它列出了制作普通对话框所需的所有必要样式。如果您不确定如何使用它们,请参阅Applying Styles and Themes。您还可以重写您的对话框类以包含使用或扩展AlertDialog.Buider
    【解决方案2】:

    我也遇到了这个问题,想创建一个和 AlertDialog 有相同 UI 的 Activity。我发现这有点困难。最后,我创建了一个透明的活动,并在其中启动了一个 AlertDialog 来解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      相关资源
      最近更新 更多