【问题标题】:Applying ThemeOverlay for custom theming a MaterialAlertDialogBuilder dialog应用 ThemeOverlay 来自定义 MaterialAlertDialogBu​​ilder 对话框
【发布时间】:2021-05-14 22:19:04
【问题描述】:

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

<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="colorOnSurface">@color/appColorOnSurface</item>
...
</style>

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

<style name="ThemeOverlay.App.Dialog.HighContrast" parent="ThemeOverlay.MaterialComponents.Dialog">
    <item name="colorOnSurface">@color/appColorOnSurfaceHighContrast</item>
</style>

并像这样应用它:

<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>

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

此外,主题似乎不是 Material,而是 AppCompat 风格。

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

【问题讨论】:

    标签: android android-theme android-dialogfragment android-dialog material-components-android


    【解决方案1】:

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

    <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(非材料)主题。

    2. 将父 ThemeOverlay 从 ThemeOverlay.MaterialComponents.Dialog.Alert 更改为 ThemeOverlay.MaterialComponents.MaterialAlertDialog 修复了主题以使其显示为 Material。

    更新:看起来ThemeOverlay.MaterialComponents.Dialog.AlertalertDialogTheme 一起使用,ThemeOverlay.MaterialComponents.MaterialAlertDialogmaterialAlertDialogTheme 一起使用。见:https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/dialog/res/values/themes.xml#L60

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 2023-03-31
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多