1, the Android system supports three kinds of fonts by default: "Sans", "serif", "monospace
2. Other fonts can be introduced in Android.
<?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" &G T </TextView> <!--using the default sans font--<textview android:id= "@+id/sans" Andro id:text= "Hello,world" android:textsize= "20sp" android:typeface= "sans" > </TextView> </TableRow> <TableRow> <textview android:layout_marginright= "4px" Androi d:text= "serif:" android:textsize= "20SP" > </TextView> <!--using the default serifs font-- <textview android:id= "@+id/serif" android:text= "Hello,world" android:textsize= "20SP" Android:typeface= "serif" > </TextView> </TableRow> <TableRow> <textview android:l ayout_marginright= "4px" android:text= "monospace:" android:textsize= "20SP" > </textview& Gt <!--use the default monospace font---<textview android:id= "@+id/monospace" android:text= "Hello, World "android:textsize=" 20SP "android:typeface=" monospace "> </TextView> </ta Blerow> <!--There is no font set, we will set it in Java code-<TableRow> <textview Android:layout_marg inright= "4px" android:text= "Custom:" android:textsize= "20SP" > </TextView> &L T TextView android:id= "@+id/custom" android:text= "Hello,world" android:textsize= "20SP" > </TextView> </TableRow></TableLayout>
Get the TextView control object TextView TextView = (textView) Findviewbyid (r.id.custom);//Save the font file in the assets/fonts/directory, Www.linuxidc.com Create 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 throughout 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);
}
}
}
}
|