How to use custom fonts in Android Development
1. The Android system supports three fonts by default: "sans", "serif", and "monospace ".
2. Other fonts can be introduced in Android.
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: layout_marginRight = "4px"
Android: text = "sans :"
Android: textSize = "20sp">
Android: id = "@ + id/sans"
Android: text = "Hello, World"
Android: textSize = "20sp"
Android: typeface = "sans">
Android: layout_marginRight = "4px"
Android: text = "serif :"
Android: textSize = "20sp">
Android: id = "@ + id/serif"
Android: text = "Hello, World"
Android: textSize = "20sp"
Android: typeface = "serif">
Android: layout_marginRight = "4px"
Android: text = "monospace :"
Android: textSize = "20sp">
Android: id = "@ + id/monospace"
Android: text = "Hello, World"
Android: textSize = "20sp"
Android: typeface = "monospace">
Android: layout_marginRight = "4px"
Android: text = "custom :"
Android: textSize = "20sp">
Android: id = "@ + id/custom"
Android: text = "Hello, World"
Android: textSize = "20sp">
// 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:
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 );
}
}
}
}
Note: For more exciting tutorials, please follow the help houseGraphic tutorialChannel,