Different font for Some activities using Calligraphy Library

江枫思渺然 提交于 2019-12-05 12:41:31

问题


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. Problem comes when I need to change font of one activity (SettingsActivity).

I tried using custom font in style however It didn't change the font of activity.

Following is the code of 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?


回答1:


Looks like it's because of AppCompat creating a different version of buttons/textviews at runtime.

In your Calligraphy startup in your Application, add the line:

.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.

Edit:

This should now be fixed with https://github.com/chrisjenx/Calligraphy/pull/191, currently as a snapshot.




回答2:


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.



来源:https://stackoverflow.com/questions/31176166/different-font-for-some-activities-using-calligraphy-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!