change font of all textviews

后端 未结 5 818
一向
一向 2021-01-03 02:28

is there a way to change the fonts of all textviews in a layout?

currently im using this to change fonts manually.

TextView txtAppName = (TextView) f         


        
5条回答
  •  生来不讨喜
    2021-01-03 03:08

    Perhaps a little late for you, but for other Android users this may be very useful.

    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). 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 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 them in your style or xml with the flFont attribute. Here's how to apply the font to all texts in your entire app:

    
    
    
        
        
        
    
        
        
        
    
        
        
        
    
    
    

    Now you can make an layout.xml looks like this:

    
    
        
    
        
    
    
    

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

提交回复
热议问题