首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为MaterialAlertDialogBuilder对话框自定义主题化应用ThemeOverlay

为MaterialAlertDialogBuilder对话框自定义主题化应用ThemeOverlay
EN

Stack Overflow用户
提问于 2021-02-11 15:14:46
回答 1查看 246关注 0票数 0

我的应用主题是这样设置的:

代码语言:javascript
复制
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="colorOnSurface">@color/appColorOnSurface</item>
...
</style>

但当我使用MaterialAlertDialogBuilder时,文本对比度非常差(因为材质对话框使用colorOnSurface with 60% alpha,而不是textColorPrimary)。所以我试着使用这个ThemeOverlay:

代码语言:javascript
复制
<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.Dialog">
    <item name="colorOnSurface">@color/appColorOnSurfaceHighContrast</item>
</style>

并像这样应用它:

代码语言:javascript
复制
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.Dialog.HighContrast</item>
    <item name="colorOnSurface">@color/appColorOnSurface</item>
    ...
</style>

但是,在对话框中显示项目列表时,这会导致问题。每个项目触摸区域仅限于显示的文本区域,而不是像正常情况下那样拉伸对话框的宽度。

此外,主题似乎不是实质性的,而是AppCompat风格。

为什么ThemeOverlay方法会导致意外的触摸区域(好像是WRAP_CONTENT)问题?这不是应用ThemeOverlay的正确方法吗?或者,有没有其他方法可以让警报对话框使用@color/appColorOnSurfaceHighContrast

EN

回答 1

Stack Overflow用户

发布于 2021-02-11 17:29:20

我通过使用ThemeOverlay.MaterialComponents.MaterialAlertDialog而不是ThemeOverlay.MaterialComponents.Dialog.Alert修复了这个问题,并且还使用了materialAlertDialogBodyTextStyle来确保只设置了对话框文本正文的样式:

代码语言:javascript
复制
<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialAlertDialogBodyTextStyle">@style/MaterialAlertDialog.App.Body.Text.HighContrast</item>
</style>

<style name="MaterialAlertDialog.App.Body.Text.HighContrast" parent="@style/MaterialAlertDialog.MaterialComponents.Body.Text">
    <item name="android:textColor">@color/appColorOnSurfaceHighContrast</item>
</style>

但是为什么AndroidStudio自动补全只显示ThemeOverlay.MaterialComponents.Dialog.Alert而不显示ThemeOverlay.MaterialComponents.MaterialAlertDialog

注意:这里实际上有两个问题:

  1. 将父ThemeOverlay从ThemeOverlay.MaterialComponents.Dialog更改为ThemeOverlay.MaterialComponents.Dialog.Alert修复了触摸区域问题,但我仍然得到一个AppCompat (非材料)主题。

  1. 将父ThemeOverlay从ThemeOverlay.MaterialComponents.Dialog.Alert更改为ThemeOverlay.MaterialComponents.MaterialAlertDialog修复了主题,使其显示为材料。

更新:看起来ThemeOverlay.MaterialComponents.Dialog.Alert是用于alertDialogTheme的,而ThemeOverlay.MaterialComponents.MaterialAlertDialog是用于materialAlertDialogTheme的。请参阅:https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/dialog/res/values/themes.xml#L60

然而,这仍然不能解释为什么后者不能自动完成。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66150262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档