本文介绍了如何正确更改 MaterialAlertDialog 文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试仅使用 Material Components 中的小部件,但在许多情况下,并没有记录如何实现样式化.

I try to use widgets from Material Components only, but in many cases, it's not documented how styling can be achieved.

让我们考虑 MaterialAlertDialog.

每次我想显示一个对话框时,我都会调用这部分代码:

Each time I want to show a dialog, I call such part of the code:

MaterialAlertDialogBuilder(context, R.style.Theme_MyApp_Dialog_Alert)
    .setTitle("Title")
    .setMessage("This is message.")
    .setPositiveButton(R.string.ok) { _, _ -> }
    .show()

如您所见,我使用的是自定义主题.

As you can see, I'm using a custom theme.

<style name="Theme.MyApp.Dialog.Alert" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <!-- attributes here -->
</style>

问题是某些属性不起作用.例如textColor.所以问题是如何在MaterialAlertDialog中改变body或title的文字颜色?

The problem is that some of the attributes are not working. For example textColor. So the question is how to change the body or title text color in MaterialAlertDialog?

我使用最新版本的 Material Components - 1.1.0-alpha07.

I use the newest version of Material Components - 1.1.0-alpha07.

附注.

我不确定哪个主题应该是父主题.在 Material Theme Builder 中,他们使用 @style/ThemeOverlay.MaterialComponents.Dialog.Alert 这实际上给出了对话框的旧"外观.

I'm not sure which theme should be a parent. In Material Theme Builder they use @style/ThemeOverlay.MaterialComponents.Dialog.Alert which actually gives an "old" look of dialogs.

推荐答案

改变样式如下

<style name="Theme.MyApp.Dialog.Alert" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialogText</item>
</style>

创建MaterialAlertDialogText样式并设置textColor

Create MaterialAlertDialogText style and set textColor

<style name="MaterialAlertDialogText" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
    <item name="android:textColor">@color/yourTextColor</item>
</style>

这篇关于如何正确更改 MaterialAlertDialog 文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 13:44