android - Android 主题/颜色:设置原色时,强调色完全不可见
问题描述
我的应用程序的一些概述:我正在使用 Google Material Components 库,所以我的应用程序主题有 parent Theme.MaterialComponents.DayNight
。
我遇到的问题是我的主题的颜色。这是一个问题的主要地方是在我定义的 DialogFragment 中。如果我像这样声明我的主题,并设置了明确的主要颜色、主要颜色和强调颜色:
<style name="DarkTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/colorDarkPrimary</item>
<item name="colorPrimaryDark">@color/colorDarkPrimaryDark</item>
<item name="colorAccent">@color/colorDarkAccent</item>
</style>
那么我的 DialogFragment 的按钮是完全不可见的(它们应该是 colorAccent):
问题当然是我希望能够定义自己的颜色。我试过只定义 acolorPrimary
而不是定义colorPrimaryDark
and colorAccent
,但这会导致重音文本由于某种原因完全不可见的相同问题。
解决方案
我将其保留,以便其他人可以找到它,但答案始终存在于文档中。
继承任何Theme.MaterialComponents
更改您必须赋予颜色的名称。您colorAccent
必须使用colorSecondary
.
这就是问题的一半。另一半是,在使用 MaterialComponents 时,最好使用MaterialAlertDialogBuilder
. 所以我将我的对话框生成器切换为这样定义:
val builder = MaterialAlertDialogBuilder(it, R.style.AlertDialogTheme)
最后一步是R.style.AlertDialogTheme
声明。在styles.xml 中,我定义了该主题来设置警报对话框的样式,它看起来像这样:
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.DayNight.Dialog.Alert">
<item name="buttonBarPositiveButtonStyle">@style/AlertButton</item>
<item name="buttonBarNegativeButtonStyle">@style/AlertButton</item>
<item name="buttonBarNeutralButtonStyle">@style/AlertButton</item>
</style>
所做@style/AlertButton
的只是将文本颜色设置为我们所追求的颜色:
<style name="AlertButton" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/colorDarkAccent</item>
</style>
最后,对话框按钮使用您想要的颜色。
推荐阅读
- java - 在加载正确的 AWS 凭证时,我们的记录中不存在您提供的 AWS 访问密钥 ID
- websecurity - 为什么需要跨域隔离和CORB/CORP?
- boto3 - boto3,Windows 上的凭据文件在哪里?
- opencv - 如何将 findTransformEcc 限制为具有比例但没有剪切的部分仿射变换?
- javascript - 当类由 Javascript 分配时不显示 CSS 类
- c++ - 使用运算符 << 返回值
- vue.js - 将数据传递到 vuejs 中的组件
- postgresql - 如何在 postgres 中创建跨多个索引的排名
- html - 尝试访问对话框中的特定行时表中的范围问题(Vue.js,Element-ui)
- c# - 按长度排序数组