android - 无法覆盖 AppTheme 字体
问题描述
这是我的 AppTheme 风格style.xml
<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 上设置自定义字体。
fontFamily
问题是当我设置AppTheme
然后我不能设置字体或fontFamily
任何TextView
。但是当我fontFamily
从font
. AppTheme
这是预览 -
- 带有 AppTheme 风格的字体属性
- 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 属性并使用它们。
推荐阅读
- php - 数据未使用 Laravel 保存在数据库中
- c# - 对象设置为活动但提醒未活动
- java - Java中的自定义动态时钟
- c# - Admob 插页式广告未在 Xamarin Forms Android 中显示
- rest - 禁用的 REST API 功能的 HTTP 状态代码是什么?
- ios - DownSample 与 CGImageSourceCreateThumbnailAtIndex 使内存急剧上升
- android - FileProvider 访问 Room 数据库抛出 IllegalArgumentException
- encryption - 如何在 ROKU 中添加用于加密和解密的公钥和私钥
- c# - 电子邮件地址域是否始终是 Microsoft Graph 中的租户域?
- docker - 如何在 docker 容器中挂载 EFS?