Set font at runtime, TextView

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

    You need to use Typeface:

    1. add font you wish to use to your project as asset.
    2. create Typeface object using that font:

      Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/MyFont.ttf");

    3. set typeface to the object you'd like to customize:

      TextView myTextView = (TextView)findViewById(R.id.my_text_view); myTextView.setTypeface(myFont);

提交回复
热议问题