问题描述
有什么方法可以在Android棒棒糖中使用geomanist(自定义)字体.
Is there any way to use geomanist(custom) font in android lollipop.
但是它可以在所有其他版本中使用.
But it works in all other version.
这是我的代码
在 MyApplication类
FontsOverride.setDefaultFont(this, "DEFAULT", "geomanist-lightnew.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "geomanist-lightnew.ttf");
FontsOverride.setDefaultFont(this, "SERIF", "geomanist-lightnew.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "geomanist-lightnew.ttf");
public final class FontsOverride {
public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
try {
final Field staticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
谢谢
推荐答案
我使用此库,它在所有设备上都能很好地工作
I use this Library and it works superbly on all devices
步骤:-
1)将此行添加到gradle compile 'com.github.balrampandey19:FontOnText:0.0.1'
1) Add this line in your gradle compile 'com.github.balrampandey19:FontOnText:0.0.1'
2)像这样在资产文件夹中添加字体
2) Add your font in asset folder like this
3)然后,将xml中的视图替换为
3) Then replace your view in xml with
<com.balram.library.FotTextView
android:id="@+id/vno_tv"
.
.
android:textSize="14sp"
app:font="regular.ttf" />
此行对于设置所需的自定义字体很重要app:font="regular.ttf"
this line is important to set custom font you want app:font="regular.ttf"
您可以对Buttons
Edittext
OR
如果您想在整个应用程序中使用相同的字体",则可以遵循此此处的指南
If you want to use same "Font" through whole application you can follow this Guide here
这篇关于自定义字体在棒棒糖中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!