Change TextView Font?

前端 未结 3 1670
悲哀的现实
悲哀的现实 2020-12-21 17:11

How can I change font type in Android TextView?



        
相关标签:
3条回答
  • 2020-12-21 17:26

    You can use the android:typeface attribute. For example:

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome!"
        android:typeface="sans"
        android:textSize="20sp"  />
    

    The XML attribute only allows the values normal, sans, serif, and monospace. If you want to use a different Typeface (perhaps a custom one that you ship with your app), you will have to do it in code by calling setTypeface() for the view after the TextView is loaded.

    0 讨论(0)
  • 2020-12-21 17:26

    To add to Ted Hopp's answer, if you are looking at a custom font for your TextView, in your Activity from where you reference the TextView, use this code example:

    Typeface blockFonts = Typeface.createFromAsset(getAssets(),"ROBOTO-MEDIUM_0.TTF");
    TextView txtSampleTxt = (TextView) findViewById(R.id.your_textview_id);
    txtSampleTxt.setTypeface(blockFonts);
    

    Although, before you can use this, you will need to copy the font/s of your choice in the assets folder.

    You can also look at one of my answer on SO which has a couple of websites you can use to download fonts. They are:

    http://www.google.com/webfonts

    http://www.fontsquirrel.com/

    0 讨论(0)
  • 2020-12-21 17:32

    You can programatically set the desire font by placing your font file (example.tff) style in Assets/fonts and then give your own string into fontpath variable

        String fontPath="fonts/Unkempt-Regular.ttf";
        TextView aboutus=(TextView)findViewById(R.id.aboutus);
        Typeface type=Typeface.createFromAsset(getAssets(), fontPath);
        aboutus.setTypeface(type);
    
    0 讨论(0)
提交回复
热议问题