如何改变android app的默认字体?

问题描述 投票:7回答:2

我想用非英语语言制作应用程序。因此,我需要使用非英语语言的textviews,字符串和toast。为每个文本设置字体很麻烦。有没有办法设置默认字体?当我必须用英语引用某些内容(比如电子邮件ID)时,我可以使用textview.settypeface(xyz)方法。

android fonts typeface android-typeface
2个回答
9
投票

android:custom fonts中有一个自定义字体的图书库

以下是如何使用它的示例。

在gradle中你需要把这行:

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

然后创建一个扩展应用程序的类并编写此代码:

public class App extends Application { 
@Override public void onCreate() {
super.onCreate();

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("your font path")
                .setFontAttrId(R.attr.fontPath)
                .build()
);
}
}

在activity类中将此方法放在onCreate之前:

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

在您的清单文件中写如下:

<application
android:name=".App"

它会将整个活动更改为您的字体!我简单的解决方案,干净!


1
投票

你可以通过3种方式实现这一目标。一种方法是创建一个自定义TextView并在任何地方引用它,即:

    public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}

在View.xml里面

<packagename.TypefacedTextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Hello"/>

另一种方法是在github中使用功能强大的Calligraphy库。 see this

最后,您可以使用自己的字体see this覆盖默认值

© www.soinside.com 2019 - 2024. All rights reserved.