自定义字体错误 [英] Custom typeface error

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

问题描述

我想改变应用程序的字体,我不能

I want to change the app typeface and I can't:

AndroidManifest.xml中

AndroidManifest.xml

...
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/CustomTheme"
        >
...

style.xml

style.xml

....

    <style name="CustomTheme">
        <item name="android:typeface">stheiti.ttf</item>
    </style>
....

不过,我得到一个错误:

But I get an error:

错误:错误:字符串类型不允许(在'机器人:字体'与
  值'stheiti.ttf')

error: Error: String types not allowed (at 'android:typeface' with value 'stheiti.ttf').

但我有一个rootAPP /资产/ stheiti.ttf

But I have a rootAPP/assets/stheiti.ttf

我不明白为什么得到这个错误,我在很多网站搜索的解决方案,但我可以没有发现任何有同样的问题。

I do not understand why get that error, I searched in many sites the solution but i couldn't found any with same problem

在此先感谢! :=)

推荐答案

您可以将自定义字体文本视图

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

如果你想实现整个应用程序自定义字体那么你可以的创建自定义视图并在该视图的初始化设置字体。

If you want to implement custom font in whole application then you can create your custom view and set your font at the initialize of that view.

public class MyTextView extends TextView {

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

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

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

public void init() {

    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "stheiti.ttf");
    setTypeface(tf ,1);

}

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

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