Android - 如何为整个应用程序设置自定义字体

25

仅仅因为它被标记为重复,就不能提供更好的答案:使用书法库来实现这个功能。它甚至可以改变 Toast 的字体。 - suku
你会不会只是使用 https://github.com/chrisjenx/Calligraphy? - Fattie
5个回答

22

编辑于2014年9月:

对于仍在查看这个糟糕旧答案的人,真正好的答案在这里:

https://dev59.com/0nE85IYBdhLWcg3wkkbK#16883281


旧版内容:

你最好尝试这个:

自定义Android字体

所以

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

将您的.ttf文件放在"assets"文件夹的根目录下。


这个很好用,但如果我有一个ListView,如何更改行的字体?我想可以通过XML来实现。 - Andrea Mario Lufino
这个解决方案似乎在Android 5.0上不再有效,还有其他选项吗? - Azzedine Hassaini
@AndreaMarioLufino 在你的适配器中设置这种字体对于所有的TextView都会起作用。假设你有textview1,textview2textView3,那么你只需要在它们上面使用setTypface(yourfont)即可。希望能帮到你。 - Recomer

11
你可以这样做。创建一个自定义的TextView,然后在所有需要的地方使用它。
public class MyTextView extends android.widget.TextView
{

    public MyTextView(Context context)
    {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);
        if (this.isInEditMode()) return ;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
        final String customFont = a.getString(R.styleable.SomeStyle_font);

        //Build a custom typeface-cache here!
        this.setTypeface(
            Typeface.createFromAsset(context.getAssets(), customFont)
        );  
    }
}

将此添加到attrs.xml。
<declare-styleable name="SomeStyle">
    <attr name="font" format="string" />
</declare-styleable>

然后在你的主题中这样做: 这将确保所有的文本视图都使用MyTextView样式。

<item name="android:textViewStyle">@style/MyTextView</item>

现在我们可以使用自定义属性在这个样式中定义您的自定义字体。
<style name="MyTextView" parent="@android:style/Widget.TextView">
    <item name="font">MyPathToFontInAssets.ttf</item>
</style>

每当您在项目中使用MyTextView时,它将具有您的自定义字体。
重要提示:字体不会被缓存,因此如果您计划使用此代码,则还应构建自定义字体缓存,以便您可以重新使用所有textviews的自定义字体。这将显着加快应用程序的速度!
更新:正如Amir所说,这与Custom fonts and XML layouts (Android)几乎相同,但我还使用android样式来自动在应用程序中的所有textviews上使用它。

嗨!在这一行 <item name="android:textViewStyle">@style/MyTextView</item> 中出现了错误,提示需要一个“type”属性。现在我该怎么办? - DroidLearner
字体不会被缓存,因此如果您打算使用此代码,您还应该构建自定义字体缓存。如何构建缓存? 如何构建缓存? --> 如何建立缓存? - DroidLearner
<item name="font">MyPathToFontInAssets.ttf</item>这里该如何设置路径?有示例吗?谢谢~ - NoobMe
在这里键入的内容将仅附加到资源路径。如果您想要一个文件夹,只需键入<item name="font">myfolder/myfont.ttf</item> - Jelle
我们如何恢复默认设置。我正在做类似于这样的事情:http://pastebin.com/qJqbEBqE - Shubham AgaRwal
显示剩余2条评论

10
创建一个TextView的子类并在任何地方使用它。
    public class RobotoTextView extends TextView {

    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RobotoTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
            setTypeface(tf);
        }
    }

}

使用方法

<com.test.RobotoTextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

在Java代码中,您可以将其强制转换为TextView。


7
TextView tv=(TextView)findViewById(R.id.custom);
    Typeface face=Typeface.createFromAsset(getAssets(),
                                          "fonts/Verdana.ttf");

    tv.setTypeface(face);

将字体文件放在 /res/fonts/ 文件夹中。

如果我的回答有帮助,请点赞...


0

你可以像这里建议的那样扩展TextView,并在其中设置你的字体,然后在整个应用程序中使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接