The Android system default font supports four fonts, respectively:
- Noraml (normal font, default font used by the system)
- Sans (non-liner font)
- Serif (serif font)
- Monospace (equal width font)
Other font files (*.ttf) can also be used
The difference between the following three types of fonts can be seen:
http://kb.cnblogs.com/page/192018/
first, using the system's own fonts
1. modifying fonts in XML
<!--use the default sans font--<TextViewAndroid:id="@+id/sans"android:text="Hello,world"Android:textsize="20SP"Android:typeface="Sans"/><!--use the default serifs font--<textview android:id= "@+id/serif" android:text= "Hello,World" Span class= "Hljs-attribute" >android:textsize= "20sp" Android:typeface= "serif"/> <!--use the default monospace font-- <textview android:id= "@+id/monospace" android:text= "Hello,World" android:textsize= "20sp" Android:typeface= "monospace"/>
2. Modifying fonts in Java code
First step: Get TextView instances
//获取textView实例 TextView textView = findViewById(R.id.textview);
Step two: Set the font
//设置serif字体 textView.setTypeface(Typeface.SERIF); //设置sans字体 textView.setTypeface(Typeface.SANS_SERIF); //设置monospace字体 textView.setTypeface(Typeface.MONOSPACE);
second, in Android can introduce other fonts
The first step: Create a new fonts directory under the assets directory and place the TTF font file here,
Step two: Call in the program
//实例化TextViewTextView textView = findViewById(R.id.textview);//得到AssetManagerAssetManager mgr=getAssets();//根据路径得到TypefaceTypeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf");//设置字体textView.setTypeface(tf);
setting fonts in Android via typeface