gpt4 book ai didi

android - 自定义字体在 Lollipop 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:35 29 4
gpt4 key购买 nike

有没有办法在android lollipop中使用geomanist(custom)字体。

但它适用于所有其他版本。

这是我的代码

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 compile 'com.github.balrampandey19:FontOnText:0.0.1' 中添加这一行

2) 像这样在 Assets 文件夹中添加您的字体

3) 然后用xml替换你的 View

<com.balram.library.FotTextView
android:id="@+id/vno_tv"
.
.
android:textSize="14sp"
app:font="regular.ttf" />

这一行对于设置你想要的自定义字体很重要 app:font="regular.ttf"

你可以对 Buttons Edittext 做同样的事情

如果你想在整个应用程序中使用相同的“字体”,你可以按照这个 Guide here

关于android - 自定义字体在 Lollipop 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44362798/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com