我按照他的文档指示设置默认字体:
@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>
为了使配置生效,您应该在自定义 application 类的 onCreate() 方法中设置默认字体,而不是在 Activity 中。
此外,https://github.com/chrisjenx/Calligraphy 上的说明说要通过重写 activity 中的方法来注入上下文,如下所示:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
除了 @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">
正如github的Readme文件中提到的, 此版本的 Calligraphy 已达到其生命周期,不再维护。请迁移到书法3!
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));
}