代码之家  ›  专栏  ›  技术社区  ›  Ridcully

主题.材料组件不更改对话框标题的字体

  •  1
  • Ridcully  · 技术社区  · 6 年前

    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

    0 回复  |  直到 5 年前
        1
  •  0
  •   Matej    5 年前

    android:titleTextAppearance 而不是 android:windowTitleStyle

    看起来你的问题在这段时间内得到了解决,因为从今天起,你的代码似乎对我有效。

        2
  •  0
  •   Gabriele Mariotti    4 年前

    正如你提到的 AlertDialog 创建人 MaterialAlertDialogBuilder textAppearanceSubtitle1 应用程序主题中的属性。

    对话框

    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