Adding custom Font to Theme in Android

前端 未结 6 1899
故里飘歌
故里飘歌 2020-11-29 19:35

Is there any way to add custom fonts in Themes in Android?

I have read Quick Tip: Customize Android Fonts, but here we have to programmetrically add custom font to

6条回答
  •  被撕碎了的回忆
    2020-11-29 19:51

    Unfortunately, Android doesn't provide the quick, easy and clean way you're looking for to change the font for your entire app. But recently I've looked into this matter and created some tools that allow you to change the font without any coding (you can do it all through xml, styles and even text appearances). They're based on similar solutions like you see in the other answers here, but allow for far more flexibility. You can read all about it on this blog, and see the github project here.

    Here's an example of how to apply these tools. Put all your font files in assets/fonts/. Then, declare those fonts in an xml file (e.g. res/xml/fonts.xml) and load this file early in your app with TypefaceManager.initialize(this, R.xml.fonts); (e.g., in the onCreate of your Application class). The xml file looks like this:

    
    
    
        
        
            
                aspergit
                someFont
            
            
                Aspergit.ttf
                Aspergit Bold.ttf
                Aspergit Italic.ttf
                Aspergit Bold Italic.ttf
            
        
    
        
        
            
                bodoni
                anotherFont
            
            
                BodoniFLF-Roman.ttf
                BodoniFLF-Bold.ttf
            
        
    
    
    

    Now you can use these fonts in your style or xml (provided you use the tools I mentioned above), by setting the flFont attribute in the custom TextView com.innovattic.font.FontTextView in your xml layout. Below you can see how you can apply a font to all texts in your entire app, just by editing res/values/styles.xml:

    
    
    
        
        
        
    
        
        
        
    
        
        
        
    
        
        
    
    
    

    With the accompanying res/layout/layout.xml:

    
    
        
        
    
        
        
    
        
        
    
    
    

    Don't forget to apply the theme in your Android manifest.

提交回复
热议问题