【问题标题】:Custom font not working in lollipop自定义字体在棒棒糖中不起作用
【发布时间】:2017-06-05 06:18:09
【问题描述】:

有没有办法在android棒棒糖中使用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();
    }



}




}

谢谢

【问题讨论】:

标签: android custom-font


【解决方案1】:

我使用这个Library,它在所有设备上都运行良好

步骤:-

1) 在你的 gradle 中添加这一行 compile '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" />

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

ButtonsEdittext 也可以这样做

或

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

【讨论】:

  • 我需要更改整个应用程序的风水字体。不适用于特定的文本视图。无论如何感谢您的回复。 @Akshay Katariya
  • 我认为这可能比stackoverflow.com/a/42218846/4762767更有帮助
  • (stackoverflow.com/a/42218846/4762767) 非常有用的链接。它工作正常。谢谢@AkshayKatariya
【解决方案2】:
  1. 问题的根本原因是字体文件写得不好,所以它 解决方案是重新编码有问题的字体是有道理的。您可以使用在线转换工具,例如 http://www.freefontconverter.com/.

  2. 另一种选择是使用自定义字体库,例如 https://github.com/chrisjenx/Calligraphy

dependencies {
            compile 'uk.co.chrisjenx:calligraphy:2.2.0'
        }

【讨论】:

    猜你喜欢
    • 2015-01-31
    • 2017-06-24
    • 2016-01-20
    • 2018-12-07
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多