Is it possible to set a custom font for entire of application?

后端 未结 25 2960
日久生厌
日久生厌 2020-11-22 02:44

I need to use certain font for my entire application. I have .ttf file for the same. Is it possible to set this as default font, at application start up and then use it else

25条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 03:10

    There is a great library for custom fonts in android:Calligraphy

    here is a sample how to use it.

    in Gradle you need to put this line into your app's build.gradle file:

    dependencies {
        compile 'uk.co.chrisjenx:calligraphy:2.2.0'
    }
    

    and then make a class that extends Application and write this code:

    public class App extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
    
            CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("your font path")
                            .setFontAttrId(R.attr.fontPath)
                            .build()
            );
        }
    } 
    

    and in the activity class put this method before onCreate:

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

    and the last thing your manifest file should look like this:

    
    

    and it will change the whole activity to your font! it's simple and clean!

提交回复
热议问题