What is the equivalent of “android:fontFamily=”sans-serif-light" in Java code?

后端 未结 6 1767
半阙折子戏
半阙折子戏 2020-12-23 15:46

My question is quite simple:

In every of my textview, I am currently using the attribute

android:fontFamily=\"sans-serif-light\"

相关标签:
6条回答
  • 2020-12-23 16:04

    It should be possible with setTypeface() and Typeface.create():

    convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
    

    See Docs:

    Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (getStyle()) to discover what its "real" style characteristics are.

    Note that excessively using Typeface.create() is bad for your memory, as stated in this comment. The suggested Hashtable is a good solution, but you have to modify it a little since you don't create your typeface from an asset.

    0 讨论(0)
  • 2020-12-23 16:12

    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

    0 讨论(0)
  • 2020-12-23 16:14

    Android 4.1 (API Level 16) and Support Library 26 and higher

    If you are using res -> font folder, you can use like this

      val typeface = ResourcesCompat.getFont(Context, R.font.YOUR_FONT)
      TextView.setTypeface(typeface)
    
    0 讨论(0)
  • 2020-12-23 16:22

    It would be possible by using

    setTypeface(Typeface tf, int style) method of TextView class.

    spinner_text.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL);

    0 讨论(0)
  • 2020-12-23 16:25

    In my opinion there is still a way to apply system fonts programatically on TextView without having any memory issue and that is using textview.setTextAppearance method :

    <style name="styleA">
        <item name="android:fontFamily">sans-serif</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">?android:attr/textColorPrimary</item>
    </style>
    <style name="styleB">
        <item name="android:fontFamily">sans-serif-light</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">?android:attr/textColorTertiary</item>
    </style>
    
    
    if(condition){
        textView.setTextAppearance(context,R.style.styleA);
    }else{
        textView.setTextAppearance(context,R.style.styleB);
    }
    
    0 讨论(0)
  • 2020-12-23 16:29

    Option 1 - API 26 and higher

    // Jave
    Typeface typeface = getResources().getFont(R.font.myfont);
    textView.setTypeface(typeface);
    
    // Kotlin
    val typeface = resources.getFont(R.font.myfont)
    textView.typeface = typeface
    

    Option 2 - API 16 and higher

    // Java
    Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
    
    // Kotlin
    val typeface = ResourcesCompat.getFont(context, R.font.myfont)
    

    Check the full expiation at Android Developers Guide.

    0 讨论(0)
提交回复
热议问题