Chrisjenx的书法库无法正常工作。

19

我按照他的文档中关于设置默认字体的指示进行了操作:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupDefaultFont();

        setContentView(R.layout.activity_main);

        setupToolbarAndNavigationDrawer();
  }

  public void setupDefaultFont() {
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/OpenSans-Regular.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );
  }

我还将字体放在assets/fonts目录下,但并没有起作用。 Roboto仍然显示为默认字体而不是Open Sans。我尝试手动逐个应用到每个TextView上,但仍然不起作用。

有什么想法为什么这不起作用吗?

更多信息:(如果有用的话) 我的miniSdkVersion是15,targetSdkVersion是22。 这些是我的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'de.hdodenhof:circleimageview:1.2.1'
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
}

这是我正在使用的自定义主题。

<resources>
    <style name="myIIT_theme" parent="Theme.AppCompat">
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorPrimary">@color/primary</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:windowBackground">@color/tertiary_dark</item>
        <item name="android:activatedBackgroundIndicator">@drawable/selected_drawer</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>
4个回答

39
为使配置生效,您应该在自定义的应用程序类的onCreate()方法中设置默认字体,而不是在活动中进行设置。
此外,https://github.com/chrisjenx/Calligraphy上的说明表示,通过覆盖活动中的一个方法将其注入到上下文中。
@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

2
无论如何还是谢谢!在你回答之前我已经找到了答案。本来应该自己在这里回答的。不过还是谢谢你。 - anobilisgorse
那我们需要在每个Activity中覆盖attachBaseContext()方法吗? - c0dehunter
1
是的。或者创建一个通用的超类,并从中继承每个Activity。 - Theo Lassonder
在Android API 26中,AppCompat视图中它不再起作用了! - Criss

4
除了 @Theo 的回答之外,确保在清单中注册您的自定义应用程序。
<application
  android:name=".MyApplication" <----------- HERE
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">

1

如在github的Readme文件中提到的那样, 这个版本的Calligraphy已经达到了生命周期的尽头,不再维护。请迁移到Calligraphy 3


0
1. 请使用以下代码更新您的代码:
 ViewPump.init(ViewPump.builder()
                .addInterceptor(new CalligraphyInterceptor(
                        new CalligraphyConfig.Builder()
                                .setDefaultFontPath("fonts/century_gothic.ttf")
                                .setFontAttrId(io.github.inflationx.calligraphy3.R.attr.fontPath)
                                .build()))
                .build());

将以下代码放在Class中:
@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接