我想在主题中使用 chrisjenx/Calligraphy 来更改整个项目中的字体。
我按照说明进行操作,但它对我不起作用
这是我的代码:
在应用中:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(base));
}
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("BElham.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
这是我的风格
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">>
<item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Widget"/>
<style name="AppTheme.Widget.TextView" parent="android:Widget.TextView">
<item name="fontPath">BElham.ttf</item>
</style>
最佳答案
您必须为每个 Activity 编写以下代码,而不是在应用程序类中。 最好将其放在 BaseActivity 中并在所有其他 Activity 中扩展它
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(base));
}
我能够使用您的代码并在我这边实现。
关于Android chrisjenx/Calligraphy 使用 Theme.AppCompat 在主题中定义的自定义字体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36373149/