How to change fontFamily of TextView in Android

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

    There is a nice library available for this

        implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
    
    0 讨论(0)
  • 2020-11-22 01:53

    It's the same as android:typeface.

    built-in fonts are:

    • normal
    • sans
    • serif
    • monospace

    See android:typeface.

    0 讨论(0)
  • 2020-11-22 01:54

    If you are using Android Studio 3.5+, Changing font is super simple. Select the text widget on Design view and check fontFamily on Attribute Window. The value dropdown contains all the available fonts from which you can select one. If you are looking for Google Fonts, Click More Fonts option.

    Attribute Window

    Google Fonts

    0 讨论(0)
  • 2020-11-22 01:55

    Try this:

    TextView textview = (TextView) findViewById(R.id.textview);
    
    Typeface tf= Typeface.createFromAsset(getAssets(),"fonts/Tahoma.ttf");
    textview .setTypeface(tf);
    
    0 讨论(0)
  • 2020-11-22 01:56

    I just want to mention that the hell with the fonts inside Android is about to end, because this year on Google IO we finally got this -> https://developer.android.com/preview/features/working-with-fonts.html

    Now there is a new resource type a font and you can place all your application fonts inside res/fonts folder and access then with R.font.my_custom_font, just like you can access string res values, drawable res values etc. You have even chance to create font-face xml file, which is gonna be set of your custom fonts (about italic, bold and underline attr).

    Read the link above for more info. Let's see the support.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题