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

Asw*_*mar 40 android android-theme android-alertdialog android-styles

我已经突破了这个问题.我需要做的是,AlertDialog在我的Android应用程序中更改所有s 的样式- 对话框背景需要是white-ish,文本需要是black-ish.我尝试创建了很多样式,主题,并从代码,清单等应用,但没有成功,关于内部的文本颜色AlertDialog.现在,我有最简单的代码,设置如下:

表现:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

    <!-- 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>
Run Code Online (Sandbox Code Playgroud)

DialogWindowTitle定义如下:

<style name="DialogWindowTitle">
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
Run Code Online (Sandbox Code Playgroud)

所以这些都不起作用.任何人都可以告诉我我可能做错了什么,我怎么能:

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

注意:我需要向上支持API 8(2.2).此外,我已经完成了大部分相关问题,并且谷歌小组,但无法弄清楚,虽然我有一种感觉它在我的鼻子下!

编辑:添加屏幕截图:

AlertDialog没有预期的主题

Dav*_*and 48

您需要为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>
Run Code Online (Sandbox Code Playgroud)

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

  • 博客文章让我得到了我需要的东西.我应该一直使用android:textColorPrimary和android:textColorAlertDialogListItem来设置对话框的内容的样式. (3认同)

Ter*_*mas 14

删除面板背景

 <item name="android:windowBackground">@color/transparent_color</item> 
 <color name="transparent_color">#00000000</color>
Run Code Online (Sandbox Code Playgroud)

这是我的风格:

 <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>
Run Code Online (Sandbox Code Playgroud)

我已经添加到构造函数中.

添加textColor:

<item name="android:textColor">#ff0000</item>
Run Code Online (Sandbox Code Playgroud)


gen*_*l03 9

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

builder = new AlertDialog.Builder(this, R.style.DialogStyle);
Run Code Online (Sandbox Code Playgroud)


Anu*_*pta 9

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.alertDialog);
Run Code Online (Sandbox Code Playgroud)

输出代码


Ste*_*oM5 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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 这是代码的java版本 - `int textColorId = getResources().getIdentifier("alertMessage", "id", "android"); TextView textColor = (TextView)alertDialog.findViewById(textColorId); if (textColor != null) { textColor.setTextColor(Color.RED); }` (2认同)