Android uses the custom font method

  • 2020-12-19 21:11:51
  • OfStack

The example in this article shows how Android uses custom fonts. To share for your reference, the details are as follows:

1. The question is:

As a beginner of android, I am making a game. The title of the game is TextView. android only provides bold italics, but the interface of the game is better if the font is set as a regular script.

2. Solutions:

Use a custom font
android Typeface uses the TTF font file to set fonts

We can put the ttf font file in the program and use Typeface to set the font in the program.

Step 1: Create a new fonts directory under assets and place the ttf font file there.

Step 2: Call in the program:


AssetManager mgr=getAssets();// get AssetManager
Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");// According to the path Typeface
tv.setTypeface(tf);// Set the font 

Using android:textStyle="bold" in xml file can set English to bold, but not Chinese to bold.
The way to set Chinese to bold is:


TextView tv = (TextView)findViewById(R.id.TextView01);
tv.getPaint().setFakeBoldText(true);
// Chinese copy " The bold "-- use TextPaint The imitation " The bold " Set up the setFakeBoldText for true . 

I hope this article has been helpful for Android programming.


Related articles: