android - 如何为整个 Android 应用设置默认字体系列

标签 android android-fonts

我在我的应用中使用 Roboto 浅色字体。要设置字体,我必须将 android:fontFamily="sans-serif-light" 添加到每个 View 中。有没有办法将 Roboto 字体声明为整个应用程序的默认字体系列?我试过这样,但似乎没有用。

<style name="AppBaseTheme" parent="android:Theme.Light"></style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

最佳答案

答案是肯定的。

TextViewButton 类的全局 Roboto 灯:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="android:buttonStyle">@style/RobotoButtonStyle</item>
</style>

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

<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

只需从列表中选择您想要的样式themes.xml ,然后在原始样式的基础上创建您的自定义样式。最后,将样式应用为应用程序的主题。

<application
    android:theme="@style/AppTheme" >
</application>

它只适用于像 Roboto 这样的内置字体,但这就是问题所在。 对于自定义字体(例如从 Assets 加载),此方法不起作用。

编辑 08/13/15

如果您使用 AppCompat 主题,请记住删除 android: 前缀。例如:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="buttonStyle">@style/RobotoButtonStyle</item>
</style>

注意 buttonStyle 不包含 android: 前缀,但 textViewStyle 必须包含它。

关于android - 如何为整个 Android 应用设置默认字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16404820/

相关文章:

android - 如何在我的 Android 应用程序中使用新的 Google Sans 字体?

android - PagerSlidingTabStrip 改变标题的字体

android - 字体文件的使用方法

android - android 中的 Houghlines 与 opencv

java - NullpointerException 尝试调用虚拟方法 'void android.widget.Button.setOnClickListener

android - list 合并 : It is possible to see the resulting mixed Manifest?

android - 如何使用 ShowcaseView android 库更改标题字体

android - 为微调器android的选定项目设置外部字体

android - 单击按钮一次打开文件夹(SD 卡)中的所有图像

android - 无法在 AndEngine 中加载 Sprite