When doing Android development, some software will require some special fonts, we need to introduce the external TTF format font into the program, the specific steps are:
Create a new fonts directory in the Android app directory assets, copy the TTF font file we need to use (the OTF format directly changes the suffix to TTF), then the code:
// Save the font file in the assets/fonts/directory, in program Typeface Typeface = Typeface.createfromasset (Getassets (), "Fonts/droidsansthai.ttf" ); // textview.settypeface (typeFace);
Ps:1. If you want to make the font bold again on this basis, use android:textstyle= "bold" in the XML file of the interface configuration is futile, the wood has the effect, at this time how to do?
We can still control in the code, plus a line of code:
Et_note.getpaint (). Setfakeboldtext (true); it can be achieved.
If you want the entire interface to use the same font, you can use batch processing to add a new Java class, as follows:
The code is used to load all TextView-based text components (TextView, buttons, RadioButton, ToggleButton, and so on) without regard to the layout hierarchy of the interface. ( Large memory waste)
In addition TTF font library files are relatively large, will increase the volume of the installation package OH
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);
}
}
}
}
更多:https://segmentfault.com/q/1010000000494116
Android References external fonts