How to change fontFamily of TextView in Android

后端 未结 30 3123
感动是毒
感动是毒 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条回答
  •  半阙折子戏
    2020-11-22 01:42

    Dynamically you can set the fontfamily similar to android:fontFamily in xml by using this,

    For Custom font:
    
     TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
     Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf"); 
     tv.setTypeface(face);
    
    For Default font:
    
     tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));
    

    These are the list of default font family used, use any of this by replacing the double quotation string "sans-serif-medium"

    FONT FAMILY                    TTF FILE                    
    
    1  casual                      ComingSoon.ttf              
    2  cursive                     DancingScript-Regular.ttf   
    3  monospace                   DroidSansMono.ttf           
    4  sans-serif                  Roboto-Regular.ttf          
    5  sans-serif-black            Roboto-Black.ttf            
    6  sans-serif-condensed        RobotoCondensed-Regular.ttf 
    7  sans-serif-condensed-light  RobotoCondensed-Light.ttf   
    8  sans-serif-light            Roboto-Light.ttf            
    9  sans-serif-medium           Roboto-Medium.ttf           
    10  sans-serif-smallcaps       CarroisGothicSC-Regular.ttf 
    11  sans-serif-thin            Roboto-Thin.ttf             
    12  serif                      NotoSerif-Regular.ttf       
    13  serif-monospace            CutiveMono.ttf              
    

    "mycustomfont.ttf" is the ttf file. Path will be in src/assets/fonts/mycustomfont.ttf , you can refer more about default font in this Default font family

提交回复
热议问题