Set font at runtime, TextView

前端 未结 9 673
温柔的废话
温柔的废话 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:10

    you can use your font which you have store in font "res/font" ex. for API level 16 and above.

       Typeface typeface = ResourcesCompat.getFont(context, R.font.rubik_medium);
       txtView.setTypeface(typeface);
    

    you can also use

       Typeface typeface = getResources().getFont(R.font.rubik_medium);
       txtView.setTypeface(typeface);
    

    but it support with API level 26 and above.

提交回复
热议问题