40

我已经为此感到头疼了。我需要做的是,更改AlertDialog我的 android 应用程序中所有 s 的样式 - 对话框背景需要是白色的,文本需要是黑色的。我尝试从代码、清单等创建很多样式、主题和应用,但没有成功,关于AlertDialog. 现在,我有最简单的代码,设置如下:

显现:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

样式.xml:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:alertDialogStyle">@style/DialogStyle</item>
</style>

<style name="DialogStyle" parent="@android:style/Theme.Dialog">
    <!-- changing these background stuff works fine -->
    <item name="android:bottomBright">@android:color/white</item>
    <item name="android:bottomDark">@android:color/white</item>
    <item name="android:bottomMedium">@drawable/dialog_footer_bg</item>
    <item name="android:centerBright">@android:color/white</item>
    <item name="android:centerDark">@drawable/dialog_body_bg</item>
    <item name="android:centerMedium">@android:color/white</item>
    <item name="android:fullBright">@color/orange</item>
    <item name="android:fullDark">@color/orange</item>
    <item name="android:topBright">@color/green</item>
    <item name="android:topDark">@drawable/dialog_header_bg</item>

下面列出的项目不起作用(请阅读我在每个元素上方的评论):

    <!-- panelBackground is not getting set to null, there is something squarish around it -->
    <item name="android:panelBackground">@null</item>

    <!-- Setting this textColor doesn't seem to have any effect at all. Messages, title, button text color, whatever; nothing changes. -->
    <item name="android:textColor">#000000</item>

    <!-- Also tried with textAppearance, as follows. Didn't work -->
    <item name="android:textAppearance">?android:attr/textColorPrimaryInverse</item>

    <!-- Also tried changing textAppearancePrimary, to no avail -->
    <item name="android:textColorPrimary">#000000</item>

    <!-- Also need to change the dialog title text, tried it as follows, dint work: -->
    <item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
</style>

DialogWindowTitle 定义如下:

<style name="DialogWindowTitle">
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>

所以这些都不起作用。谁能告诉我我做错了什么,我该怎么做:

  1. 更改消息的文本颜色(内容文本)
  2. 更改标题文本颜色
  3. 移除面板背景

注意:我需要向上支持 API 8 (2.2)。另外,我在这里和谷歌群组中讨论了大部分相关问题,但无法弄清楚,尽管我感觉它就在我的鼻子底下!

编辑:添加截图:

AlertDialog 未按预期主题

4

8 回答 8

50

您需要为您的 AlertDialog 定义一个主题并在您的活动主题中引用它。属性 isalertDialogTheme和 not alertDialogStyle。像这样:

<style name="Theme.YourTheme" parent="@android:style/Theme.Holo">
    ...
    <item name="android:alertDialogTheme">@style/YourAlertDialogTheme</item>
</style>

<style name="YourAlertDialogTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    <item name="android:windowTitleStyle">...</item>
    <item name="android:textAppearanceMedium">...</item>
    <item name="android:borderlessButtonStyle">...</item>
    <item name="android:buttonBarStyle">...</item>
</style>

您将能够更改标题、消息的颜色和文本外观,并且您将对每个区域的背景进行一些控制。我写了一篇博文,详细介绍了设置 AlertDialog 样式的步骤。

于 2014-07-03T17:35:05.640 回答
14

移除面板背景

 <item name="android:windowBackground">@color/transparent_color</item> 
 <color name="transparent_color">#00000000</color>

这是我的风格:

 <style name="ThemeDialogCustom">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@color/transparent_color</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

我已将其添加到构造函数中。

添加 textColor :

<item name="android:textColor">#ff0000</item>
于 2013-11-05T08:46:37.667 回答
10

这是我的代码来主题警报对话框:

<style name="alertDialog" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:background">@color/light_button_text_color</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:textColorSecondary">@android:color/black</item>
    <item name="android:titleTextColor" tools:targetApi="m">@android:color/black</item>
</style>

将此代码放在styles.xml 中。在您的 java 中将此主题应用为:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.alertDialog);

代码的输出

于 2017-09-27T08:36:08.057 回答
9

您必须将样式添加到对话框的构造函数中

builder = new AlertDialog.Builder(this, R.style.DialogStyle);
于 2013-11-05T08:40:15.917 回答
5

我以这种方式以编程方式更改颜色:

var builder = new AlertDialog.Builder (this);
...
...
...
var dialog = builder.Show ();
int textColorId = Resources.GetIdentifier ("alertTitle", "id", "android");
TextView textColor = dialog.FindViewById<TextView> (textColorId);
textColor?.SetTextColor (Color.DarkRed);

作为 alertTitle,您可以通过这种方式更改其他数据(下一个示例是 titleDivider):

int titleDividerId = Resources.GetIdentifier ("titleDivider", "id", "android");
View titleDivider = dialog.FindViewById (titleDividerId);
titleDivider?.SetBackgroundColor (Color.Red);

这是在 C# 中,但在 java 中是一样的。

于 2016-08-25T10:02:52.773 回答
3

这取决于您想要自定义警报对话框的程度。我有不同的步骤来自定义警报对话框。请访问:https ://stackoverflow.com/a/33439849/5475941

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

于 2016-09-24T21:58:35.307 回答
2

基于@general03 的回答,您可以使用Android 的内置样式快速自定义对话框。您可以在 下找到对话框主题android.R.style.Theme_DeviceDefault_Dialogxxx

例如:

builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_MinWidth);
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_DialogWhenLarge);
于 2015-04-24T05:40:24.203 回答
1

在你的values-v21/style.xml的 Style 中使用它

<style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:colorAccent">@color/cbt_ui_primary_dark</item>
        <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sphinx</item>
        <item name="android:textColorPrimary">@color/cbt_hints_color</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

对于棒棒糖之前的设备,将其放入values/style.xml

<style name="AlertDialogCustom" parent="@android:style/Theme.Material.Dialog.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:colorAccent">@color/cbt_ui_primary_dark</item>
        <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sphinx</item>
        <item name="android:textColorPrimary">@color/cbt_hints_color</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

<style name="DialogWindowTitle.Sphinx" parent="@style/DialogWindowTitle_Holo">
       <item name="android:textAppearance">@style/TextAppearance.Sphinx.DialogWindowTitle</item>
</style>

<style name="TextAppearance.Sphinx.DialogWindowTitle" parent="@android:style/TextAppearance.Holo.DialogWindowTitle">
        <item name="android:textColor">@color/dark</item>
        <!--<item name="android:fontFamily">sans-serif-condensed</item>-->
        <item name="android:textStyle">bold</item>
</style>

于 2017-01-10T11:16:59.760 回答