在 Android 中为主题添加自定义字体 [英] Adding custom Font to Theme in Android

查看:21
本文介绍了在 Android 中为主题添加自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在 Android 的 Themes 中添加自定义字体?

Is there any way to add custom fonts in Themes in Android?

我已阅读快速提示:自定义 Android 字体,但是在这里,我们必须以编程方式向文本添加自定义字体.

I have read Quick Tip: Customize Android Fonts, but here we have to programmetrically add custom font to text.

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font); 

但我想按样式/主题设置自定义字体.

But I want to set the custom font by style/theme.

推荐答案

不幸的是,Android 没有提供您正在寻找的快速、简单和干净的方式来更改整个应用程序的字体.但最近我研究了这个问题并创建了一些工具,允许您无需任何编码即可更改字体(您可以通过 xml、样式甚至文本外观来完成所有操作).它们基于类似的解决方案,就像您在此处的其他答案中看到的一样,但允许更大的灵活性.您可以在 本博客 上阅读所有相关内容,并查看 github 项目 此处.

Unfortunately, Android doesn't provide the quick, easy and clean way you're looking for to change the font for your entire app. But recently I've looked into this matter and created some tools that allow you to change the font without any coding (you can do it all through xml, styles and even text appearances). They're based on similar solutions like you see in the other answers here, but allow for far more flexibility. You can read all about it on this blog, and see the github project here.

以下是如何应用这些工具的示例.将所有字体文件放在 assets/fonts/ 中.然后,在 xml 文件(例如 res/xml/fonts.xml)中声明这些字体,并使用 TypefaceManager.initialize(this, R.xml.fonts) 在您的应用程序中尽早加载此文件;(例如,在您的 Application 类的 onCreate 中).xml 文件如下所示:

Here's an example of how to apply these tools. Put all your font files in assets/fonts/. Then, declare those fonts in an xml file (e.g. res/xml/fonts.xml) and load this file early in your app with TypefaceManager.initialize(this, R.xml.fonts); (e.g., in the onCreate of your Application class). The xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<familyset>

    <!-- Some Font. Can be referenced with 'someFont' or 'aspergit' -->
    <family>
        <nameset>
            <name>aspergit</name>
            <name>someFont</name>
        </nameset>
        <fileset>
            <file>Aspergit.ttf</file>
            <file>Aspergit Bold.ttf</file>
            <file>Aspergit Italic.ttf</file>
            <file>Aspergit Bold Italic.ttf</file>
        </fileset>
    </family>

    <!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' -->
    <family>
        <nameset>
            <name>bodoni</name>
            <name>anotherFont</name>
        </nameset>
        <fileset>
            <file>BodoniFLF-Roman.ttf</file>
            <file>BodoniFLF-Bold.ttf</file>
        </fileset>
    </family>

</familyset>

现在您可以在您的样式或 xml 中使用这些字体(前提是您使用我上面提到的工具),方法是在您的自定义 TextView com.innovattic.font.FontTextView 中设置 flFont 属性xml布局.您可以在下面看到如何将字体应用于整个应用程序中的所有文本,只需编辑 res/values/styles.xml:

Now you can use these fonts in your style or xml (provided you use the tools I mentioned above), by setting the flFont attribute in the custom TextView com.innovattic.font.FontTextView in your xml layout. Below you can see how you can apply a font to all texts in your entire app, just by editing res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

    <!-- Application theme -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    </style>

    <!-- Style to use for ALL text views (including FontTextView) -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView">
        <item name="android:textAppearance">@style/MyTextAppearance</item>
    </style>

    <!-- Text appearance to use for ALL text views (including FontTextView) -->
    <!-- Use a different parent if you don't want Holo Light -->
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo">
        <!-- Alternatively, reference this font with the name "aspergit" -->
        <!-- Note that only our own TextView's will use the font attribute -->
        <item name="flFont">someFont</item>
        <item name="android:textStyle">bold|italic</item>
    </style>

    <!-- Alternative style, maybe for some other widget -->
    <style name="StylishFont">
        <item name="flFont">anotherFont</item>
        <item name="android:textStyle">normal</item>
    </style>

</resources>

随附res/layout/layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <!-- This text view is styled with the app theme -->
    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This uses my font in bold italic style" />

    <!-- This text view is styled here and overrides the app theme -->
    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:flFont="anotherFont"
        android:textStyle="normal"
        android:text="This uses another font in normal style" />

    <!-- This text view is styled with a style and overrides the app theme -->
    <com.innovattic.font.FontTextView
        style="@style/StylishFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This also uses another font in normal style" />

</LinearLayout>

不要忘记在您的 Android 清单中应用主题.

Don't forget to apply the theme in your Android manifest.

这篇关于在 Android 中为主题添加自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆