1, the Android system defaults to support three kinds of fonts, respectively: "Sans", "serif", "monospace
2, in Android can introduce other fonts.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<TableRow>
<textview
android:layout_marginright= "4px"
android:text= "sans:"
Android: Textsize= "20SP"
</textview>
<!--Use the default sans font;
<textview
Android:id= "@+id/sans"
android:text= "Hello,world"
Android:textsize= "20SP"
Android:typeface= "Sans" >
</TextView>
</TableRow>
<TableRow>
<textview
android:layout_marginright= "4px"
android:text= "serif:"
Android : textsize= "20sp"
</textview>
<!--Use the default serifs font;
<textview
Android:id= "@+id/serif"
android:text= "Hello,world"
Android:textsize= "20SP"
android:typeface= "Serif" >
</TextView>
</TableRow>
<TableRow>
<textview
android:layout_marginright= "4px"
android:text= "monospace:"
Android:textsize= "20SP"
</textview>
<!--Use the default monospace font;
<textview
Android:id= "@+id/monospace"
android:text= "Hello,world"
Android:textsize= "20SP"
Android:typeface= "Monospace" >
</TextView>
</TableRow>
<!--there is no font set here, we will set--> in Java code
<TableRow>
<textview
android:layout_marginright= "4PX"
android:text= "Custom:"
Android:textsize= "20SP" >
</TextView>
<textview
Android:id= "@+id/custom"
android:text= "Hello,world"
Android:textsize= "20SP" >
</TextView>
</TableRow>
</TableLayout>
Copy Code code as follows:
Get TextView Control Object
TextView TextView = (TextView) Findviewbyid (R.id.custom);
Save the font file in the assets/fonts/directory, www.linuxidc.com create the typeface object
Typeface typeface = Typeface.createfromasset (Getassets (), "Fonts/droidsansthai.ttf");
Apply Font
Textview.settypeface (typeface);
if you want to apply a custom font to all controls on the entire interface, you can:
Copy Code code as follows:
Package arui.blog.csdn.net;
Import android.app.Activity;
Import Android.graphics.Typeface;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
public class Fontmanager {
public static void Changefonts (ViewGroup root, Activity Act) {
typeface tf = Typeface.createfromasset (Act.getassets (),
"Fonts/xxx.ttf");
for (int i = 0; i < Root.getchildcount (); i++) {
View v = root.getchildat (i);
if (v instanceof TextView) {
((TextView) v). Settypeface (TF);
else if (v instanceof Button) {
((Button) v). Settypeface (TF);
else if (v instanceof edittext) {
((EditText) v). Settypeface (TF);
else if (v instanceof viewgroup) {
Changefonts ((viewgroup) v, ACT);
}
}
}
}