gpt4 book ai didi

android - Theme.MaterialComponents 不会更改对话框标题的字体

转载 作者:太空狗 更新时间:2023-10-29 14:38:10 25 4
gpt4 key购买 nike

我正在为我们的应用程序使用 Material 组件主题。现在我们想要一种自定义字体,我设法将其应用于下面主题的几乎所有地方,它使用 Material 组件定义的各种 textAppearance... 属性。

这非常有效,并且主题也几乎无处不在地应用于 AlertDialogs——消息文本和按钮具有自定义字体,按钮具有正确的强调色等。

无论如何,只有对话框标题保留 Roboto 字体。

dialog with correct colors but wrong title font

<!-- externalized font name for easier change -->
<string name="font_regular" translatable="false" tools:ignore="ReferenceType">@font/atma_regular</string>

<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textColorSecondary">@color/textColorSecondary</item>

<item name="textAppearanceHeadline1">@style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline2">@style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline3">@style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline4">@style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline5">@style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline6">@style/TextAppearance.App.Button</item>
<item name="textAppearanceSubtitle1">@style/TextAppearance.App.Subtitle1</item>
<item name="textAppearanceSubtitle2">@style/TextAppearance.App.Subtitle2</item>
<item name="textAppearanceBody1">@style/TextAppearance.App.Body1</item>
<item name="textAppearanceBody2">@style/TextAppearance.App.Body2</item>
<item name="textAppearanceButton">@style/TextAppearance.App.Button</item>
<item name="android:textAppearanceLarge">@style/TextAppearance.App.Large</item>
<item name="android:textAppearanceMedium">@style/TextAppearance.App.Medium</item>
<item name="android:textAppearanceSmall">@style/TextAppearance.App.Small</item>
</style>

<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">@string/font_regular</item>
<item name="android:fontFamily">@string/font_regular</item>
</style>
...

我试着像这样为警告对话框定义一个额外的主题:

<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="alertDialogTheme">@style/Theme.App.AlertDialog</item>
...
</style>

<style name="Theme.App.AlertDialog" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="android:windowTitleStyle">@style/TextAppearance.App.DialogWindowTitle</item>
</style>

<style name="TextAppearance.App.DialogWindowTitle" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="fontFamily">@string/font_regular</item>
<item name="android:fontFamily">@string/font_regular</item>
<item name="android:textSize">30sp</item>
</style>

但这会重置各处的颜色和字体。唯一应用的是 textSize。

custom dialog theme makes it worse

实现这个真的不应该那么难,但我现在没有想法。我可以以编程方式应用字体,但这会很丑陋。

最佳答案

正如您提到的,由 MaterialAlertDialogBu​​ilder 创建的 AlertDialog 使用您应用中的 textAppearanceSubtitle1 属性定义的样式主题。

否则你可以改变AlertDialog使用的风格,使用类似:

new MaterialAlertDialogBuilder(MainActivity.this,
R.style.MyTitle_ThemeOverlay_MaterialComponents_MaterialAlertDialog)
.setTitle("Title")
.setMessage("Message......")
.setPositiveButton("ok", null)
.setNegativeButton("Cancel", null)
.show();

在您的样式中,您必须自定义 materialAlertDialogTitleTextStyle 属性:

  <style name="MyTitle_ThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogTitleTextStyle">@style/MyTitle_MaterialAlertDialog.MaterialComponents.Title.Text</item>
</style>
<style name="MyTitle_MaterialAlertDialog.MaterialComponents.Title.Text" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/MyTitle_TextAppearance.MaterialComponents.Subtitle1</item>
</style>
<style name="MyTitle_TextAppearance.MaterialComponents.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">....</item>
<item name="android:fontFamily">....</item>
<item name="android:textStyle">.....</item>
</style>

enter image description here

关于android - Theme.MaterialComponents 不会更改对话框标题的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com