简体   繁体   English

使用书法图书馆的一些活动的不同字体

[英]Different font for Some activities using Calligraphy Library

I am using Calligraphy Library for using custom font in my application. 我正在使用书法库在我的应用程序中使用自定义字体。 I set a custom font to my whole application by default font using CalligraphyConfig , in my Application class in the #onCreate() method and it is working fine. 我使用CalligraphyConfig在我的Application类的#onCreate()方法中使用默认字体为我的整个应用程序设置了一个自定义字体,它运行正常。 Problem comes when I need to change font of one activity (SettingsActivity). 当我需要更改一个活动的字体(SettingsActivity)时出现问题。

I tried using custom font in style however It didn't change the font of activity. 我尝试在样式中使用自定义字体,但它没有更改活动的字体。

Following is the code of Style 以下是Style的代码

    <style name="EnglishActivitiesTheme" parent="AppTheme">
        <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
    </style>

    <style name="AppTheme.Widget.TextView" parent="android:Widget.Holo.Light.TextView">
        <item name="fontPath">fonts/Roboto-Regular.ttf</item>
    </style>

In Manifest 在清单中

    <activity
        android:name=".SettingsActivity"
        android:theme="@style/EnglishActivitiesTheme"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

Am I doing something wrong with custom font though style method? 我是否通过样式方法使用自定义字体做错了什么? or Is there better method of doing this? 或者有更好的方法吗?

Looks like it's because of AppCompat creating a different version of buttons/textviews at runtime. 看起来是因为AppCompat在运行时创建了不同版本的按钮/文本视图。

In your Calligraphy startup in your Application, add the line: 在您的应用程序中的Calligraphy启动中,添加以下行:

.addCustomStyle(AppCompatTextView.class, android.R.attr.textViewStyle)

to end up with something like: 最终结果如下:

@Override
public void onCreate() {
    super.onCreate();
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("fonts/some-other-custom-font.ttf")
                            .addCustomStyle(AppCompatTextView.class, android.R.attr.textViewStyle)
                            .setFontAttrId(R.attr.fontPath)
                            .build()
            );
    //....
}

And that should take care of your textviews. 这应该照顾你的textviews。

Edit: 编辑:

This should now be fixed with https://github.com/chrisjenx/Calligraphy/pull/191 , currently as a snapshot. 现在应该使用https://github.com/chrisjenx/Calligraphy/pull/191修复此问题,目前作为快照。

Not Best Solution however better than custom font in that activity. 不是最佳解决方案,但比该活动中的自定义字体更好。

I just removed 我刚刚删除

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

from activity where I was trying to change font. 从我试图改变字体的活动。 In That activity using default android font which is better than that custom font for my other activities. 在该活动中使用默认的android字体,这比我的其他活动的自定义字体更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM