1. The Android system supports three fonts by default: "sans", "serif", and "monospace ".
2. Other fonts can be introduced in Android.
Copy codeThe Code is 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>
<! -- No font is set here. We will set it 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 codeThe Code is as follows: // obtain the TextView control object.
TextView textView = (TextView) findViewById (R. id. custom );
// Save the font file to the assets/fonts/directory and create a Typeface object at www.linuxidc.com
Typeface typeFace = Typeface. createFromAsset (getAssets (), "fonts/DroidSansThai. ttf ");
// Apply the font
TextView. setTypeface (typeFace );
To apply a Custom font to all controls on the interface, you can:
Copy codeThe Code is 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 );
}
}
}
}