android - 无法覆盖 AppTheme 字体

标签 android fonts android-xml

这是我在 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标签

 <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风格的字体属性

with font in AppTheme

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

with font in AppTheme

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

最佳答案

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 属性并使用它们。

关于android - 无法覆盖 AppTheme 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50056649/

相关文章:

android - 在android中使用图像按钮设计如下图所示的布局

java - 如何从url下载图片?

Javafx Textfield 文本对齐会阻止字体更改

c++ - 获取字体列表 (Win32)

android - EditText 输入类型 textCapSentences 不适用于 Lollipop

android - AndroidX双向数据绑定(bind)中的未知类: java. lang.String

android - 我如何允许用户浏览/选择一个文件供我在 Android 中使用的应用程序使用?

android - 在Android中将声音录制到字节流中

android - '非法状态异常 : Activity has been destroyed' when 'getSupportFragmentManager()' called after Activity restart

java - Swing : font color & strike color in JTextPane