Set font at runtime, TextView

前端 未结 9 674
温柔的废话
温柔的废话 2020-11-29 22:00

How to set the font of a TextView created at runtime?

I created a TextView

Textview tv = new TextView(this);      
tv.setTextSize(20);

9条回答
  •  青春惊慌失措
    2020-11-29 22:07

    You can have .ttf font in your asset folder. Say font's name is "default.ttf" and you just now have to write below 2 lines of code

    TextView text = new TextView(this);
    text.setTypeface(Typeface.createFromAsset(getAssets(), "default.ttf"));
    

    You must also we careful because different font have different sizes. You may need to set size as :

    text.setTextSize(20);
    

提交回复
热议问题