我已经为这件事伤透了脑筋。我需要做的是,改变所有的风格AlertDialog
在我的 Android 应用程序中 - 对话框背景需要是白色的,文本需要是黑色的。我尝试创建很多样式、主题,并从代码、清单等中应用,但就文本颜色而言没有成功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>
所以这些都不起作用。谁能告诉我我可能做错了什么,我该怎么做:
- 更改消息的文本颜色(内容文本)
- 更改标题文字颜色
- 去除
面板背景
注意:我需要支持API 8(2.2)以上。另外,我已经浏览了这里的大部分相关问题和谷歌小组,但无法弄清楚,尽管我有一种感觉它就在我眼皮子底下!
编辑:添加屏幕截图:
您需要为您的 AlertDialog 定义一个主题,并在您的活动主题中引用它。属性是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>
您将能够更改标题、消息的颜色和文本外观,并且您可以对每个区域的背景进行一些控制。我写了一个博客文章 https://web.archive.org/web/20200214041924/http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/详细说明设置 AlertDialog 样式的步骤。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)