【问题标题】:Can not override AppTheme font无法覆盖 AppTheme 字体
【发布时间】:2018-10-07 23:01:58
【问题描述】:

这是我在style.xml中的AppTheme风格

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily" tools:targetApi="jelly_bean">@font/montserrat_regular</item>
    <item name="fontFamily">@font/montserrat_regular</item>
</style>

manifest.xml中的应用标签

 <application
        android:name=".Amelio"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"

我想要的是在 AppTheme 中设置 fontFamily,这样我就不必在应用程序的每个组件上都设置它。 但我想在应用程序中的一些 TextView 上设置自定义字体。

问题是当我在AppTheme 中设置fontFamily 时,我无法在任何TextView 上设置字体或fontFamily。但是当我从AppTheme 中删除fontFamilyfont 时,我可以设置它。这是预览-

  1. 在 AppTheme 样式中带有字体属性

  1. AppTheme 风格中没有字体属性

注意:我知道 font 或 fontFamily 都可以使用,如果可行,我只是在尝试两者。所以请忽略它。

【问题讨论】:

标签: android fonts android-xml


【解决方案1】:

Khemraj 建议 https://stackoverflow.com/a/16407123/6891563 是正确的,因为当你想要覆盖某些组件的属性时,你需要覆盖它的样式,所以当你添加时(取自 Hussein El Feky):

<!-- Into a styles.xml--!>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

<!-- Into a themes.xml--!>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="buttonStyle">@style/RobotoButtonStyle</item>
</style>

您正在覆盖所有 TextView 的样式,因此无论何时使用 AppTheme,TextView 组件都会监​​听 RobotoTextViewStyle 属性并使用它们。

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多