我的应用主题是这样设置的:
<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>但是,在对话框中显示项目列表时,这会导致问题。每个项目触摸区域仅限于显示的文本区域,而不是像正常情况下那样拉伸对话框的宽度。
此外,主题似乎不是实质性的,而是AppCompat风格。
为什么ThemeOverlay方法会导致意外的触摸区域(好像是WRAP_CONTENT)问题?这不是应用ThemeOverlay的正确方法吗?或者,有没有其他方法可以让警报对话框使用@color/appColorOnSurfaceHighContrast
发布于 2021-02-11 17:29:20
我通过使用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呢
注意:这里实际上有两个问题:
ThemeOverlay.MaterialComponents.Dialog更改为ThemeOverlay.MaterialComponents.Dialog.Alert修复了触摸区域问题,但我仍然得到一个AppCompat (非材料)主题。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
然而,这仍然不能解释为什么后者不能自动完成。
https://stackoverflow.com/questions/66150262
复制相似问题