How to change fontFamily of TextView in Android

后端 未结 30 3083
感动是毒
感动是毒 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:56

    The easiest way to add the font programatically to a TextView is to, first, add the font file in your Assets folder in the project. For example your font path is looking like this: assets/fonts/my_font.otf

    And add it to a TextView as:

    Kotlin

    val font_path = "fonts/my_font.otf"  
    
    myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)
    
    textView.typeface = myTypeface
    

    Java

    String font_path = "fonts/my_font.otf";
    Typeface myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)
    textView.setTypeface(myTypeface);
    

提交回复
热议问题