Android chrisjenx/Calligraphy 使用 Theme.AppCompat 在主题中定义的自定义字体不起作用

标签 android textview android-styles custom-font

我想在主题中使用 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/

相关文章:

java - 从资源 XML 文件获取 layout.xml 方向/高度/宽度属性

android - iphone 和 android 的 Css 区别?

java - Android Studio 中的 OpenCL

java - 带有 netty 服务器 (SSLSocket) 的 Android 客户端

android - 在 Android XML 中,我可以将 buttonStyleSmall 或 Button.Small 设置为自定义样式的父样式吗?

java - 为什么 '\n' 个字符会拉伸(stretch)我的 TextView?

android - TextView.setText() 不起作用

android - 如何在android中的其他两个 View 之间垂直放置一个 View ?

android - 具有旧样式和不同颜色的 TimePickerDialog

android - 为什么 `TextInputLayout` 会使用样式颜色来绘制光标而不是下划线?