android – 自定义字体在棒棒糖中不起作用
作者:互联网
有没有办法在android lollipop中使用geomanist(自定义)字体.
但它适用于所有其他版本.
这是我的代码
在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();
}
}
}
谢谢
解决方法:
我使用这个Library,它在所有设备上运行良好
步:-
1)在你的gradle编译’com.github.balrampandey19:FontOnText:0.0.1’中添加这一行
2)像这样在资产文件夹中添加你的字体
3)然后用xml替换你的视图
<com.balram.library.FotTextView
android:id="@+id/vno_tv"
.
.
android:textSize="14sp"
app:font="regular.ttf" />
此行对于设置您想要的自定义字体很重要:font =“regular.ttf”
你可以为Buttons Edittext做同样的事情
要么
如果你想在整个应用程序中使用相同的“字体”,你可以关注this Guide here
标签:custom-font,android 来源: https://codeday.me/bug/20191001/1839227.html