如何更改 android 应用程序的默认字体?
How to change default font of the android app?
我想用非英语语言制作一个应用程序。所以我需要非英语的文本视图、字符串和吐司。为每个文本设置字体很麻烦。有没有办法设置默认字体?当我必须用英语引用某些东西(比如电子邮件 ID)时,我可以使用 textview.settypeface(xyz) 方法。
您主要可以通过 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
android 中有一个自定义字体的 grate 库:custom fonts
这是一个如何使用它的示例。
在 gradle 中你需要加入这一行:
compile 'uk.co.chrisjenx:calligraphy:2.1.0'
然后制作一个扩展应用程序的class并编写此代码:
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 class中把这个方法放在onCreate之前:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
在你的清单文件中这样写:
<application
android:name=".App"
它将整个 activity 更改为您的字体!。我不是简单的解决方案和清洁!
我想用非英语语言制作一个应用程序。所以我需要非英语的文本视图、字符串和吐司。为每个文本设置字体很麻烦。有没有办法设置默认字体?当我必须用英语引用某些东西(比如电子邮件 ID)时,我可以使用 textview.settypeface(xyz) 方法。
您主要可以通过 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
android 中有一个自定义字体的 grate 库:custom fonts
这是一个如何使用它的示例。
在 gradle 中你需要加入这一行:
compile 'uk.co.chrisjenx:calligraphy:2.1.0'
然后制作一个扩展应用程序的class并编写此代码:
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 class中把这个方法放在onCreate之前:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
在你的清单文件中这样写:
<application
android:name=".App"
它将整个 activity 更改为您的字体!。我不是简单的解决方案和清洁!