Android font Settings and Roboto font usage

  • 2020-06-15 10:11:11
  • OfStack

This article illustrates Android font setting and Roboto font usage. Share to everybody for everybody reference. The specific analysis is as follows:

1. Custom fonts

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 put 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=findViewById(R.id.textview);
tv.setTypeface(tf);// Set the font

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

TextView tv = (TextView)findViewById(R.id.TextView01);   
tv.getPaint().setFakeBoldText(true);// Chinese copy of "Bold" -- use TextPaint "In bold" setFakeBoldText for true .

Note: Some fonts are not valid in Chinese. Although no error will be reported, they are not valid in Chinese.

2. Use RoBoto

Roboto has been used as the default font since Android4.0. Here's how to use it:

android:fontFamily="sans-serif" // roboto regular  
android:fontFamily="sans-serif-light" // roboto light 
android:fontFamily="sans-serif-condensed" // roboto condensed 
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2) 
//in combination with 
android:textStyle="normal|bold|italic"

The parameters available are as follows:

Regular
Italic
Bold
Bold-italic
Light
Light-italic
Thin
Thin-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic

I hope this article has been helpful for your Android programming.


Related articles: