AlertDialog样式 - 如何更改标题,消息等的样式(颜色)

时间:2013-04-24 19:50:19

标签: android android-alertdialog android-theme android-styles

我已经打破了这一点。我需要做的是,在我的android应用程序中更改所有AlertDialog的样式 - 对话框背景需要是白色的,文本需要是黑色的。我尝试创建了很多样式,主题,并从代码,清单等应用,但没有成功,关于AlertDialog内的文本颜色。现在,我有最简单的代码,设置如下:

清单:

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

styles.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. 删除 小组背景
  4. 注意:我需要向上支持API 8(2.2)。另外,我已经完成了大部分相关问题,并且谷歌小组,但无法弄清楚,虽然我有一种感觉它在我的鼻子下面!

    编辑:添加屏幕截图:

    AlertDialog not themed as expected

8 个答案:

答案 0 :(得分:47)

您需要为AlertDialog定义一个主题,并在Activity的主题中引用它。该属性为alertDialogTheme,而非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>

您将能够更改标题,消息的颜色和文本外观,并且您可以对每个区域的背景进行一些控制。我写了一个blog post详细说明了为AlertDialog设置样式的步骤。

答案 1 :(得分:14)

删除面板背景

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

这是Mystyle:

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

答案 2 :(得分:8)

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

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

答案 3 :(得分:6)

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

public class CommonUtil {
    /**
     * This function will show toast in the application
     * @param message
     */
    public static void showToast(String message) {
        if (!message.isEmpty()) {
            Toast.makeText(MyApplication.getContext(), message, Toast.LENGTH_SHORT).show();
        }
    }
}

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

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

Output of the code

答案 4 :(得分:5)

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

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

as alertTitle,您可以通过这种方式更改其他数据(下一个示例适用于titleDivider):

protected internal virtual InvalidModelStateResult BadRequest(ModelStateDictionary modelState);

这是在C#中,但在java中它是相同的。

答案 5 :(得分:2)

在@ general03&#39的答案基础上,您可以使用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);

答案 6 :(得分:2)

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

enter image description here enter image description here enter image description here enter image description here

答案 7 :(得分:1)

values-v21 / style.xml

中的样式中使用此功能

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

对于棒棒糖前设备,请将其置于 values / style.xml

&#13;
&#13;
<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>
&#13;
&#13;
&#13;