How to change fontFamily of TextView in Android

后端 未结 30 2912
感动是毒
感动是毒 2020-11-22 01:05

So I\'d like to change the android:fontFamily in Android but I don\'t see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don\'

30条回答
  •  萌比男神i
    2020-11-22 01:47

    Starting from Android-Studio 3.0 its very easy to change font family

    Using support library 26, it will work on devices running Android API version 16 and higher

    Create a folder font under res directory .Download the font which ever you want and paste it inside font folder. The structure should be some thing like below

    Note: As of Android Support Library 26.0, you must declare both sets of attributes ( android: and app: ) to ensure your fonts load on devices running Api 26 or lower.

    Now you can change font in layout using

    
    

    To change Programatically

     Typeface typeface = getResources().getFont(R.font.myfont);
       //or to support all versions use
    Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
     textView.setTypeface(typeface);  
    

    To change font using styles.xml create a style

     
    

    and apply this style to TextView

      
    

    you can also Create your own font family

    - Right-click the font folder and go to New > Font resource file. The New Resource File window appears.

    - Enter the file name, and then click OK. The new font resource XML opens in the editor.

    Write your own font family here , for example

    
        
        
    
    

    this is simply a mapping of a specific fontStyle and fontWeight to the font resource which will be used to render that specific variant. Valid values for fontStyle are normal or italic; and fontWeight conforms to the CSS font-weight specification

    1. To change fontfamily in layout you can write

     
    

    2. To Change Programmatically

     Typeface typeface = getResources().getFont(R.font.lobster);
       //or to support all versions use
    Typeface typeface = ResourcesCompat.getFont(context, R.font.lobster);
     textView.setTypeface(typeface);  
    

    To change font of entire App Add these two lines in AppTheme

     
    

    See the Documentation , Android Custom Fonts Tutorial For more info

提交回复
热议问题