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:
packagearui.blog.csdn.net; importandroid.app.Activity; importandroid.graphics.Typeface; import android.view.View; importandroid.view.ViewGroup; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.TextView; publicclassFontManager { publicstaticvoidchangeFonts(ViewGroup root, Activity act) { Typeface tf = Typeface.createFromAsset(act.getAssets(), "fonts/xxx.ttf"); for(inti = 0; i < root.getChildCount(); i++) { View v = root.getChildAt(i); if (v instanceofTextView) { ((TextView) v).setTypeface(tf); } elseif(v instanceofButton) { ((Button) v).setTypeface(tf); } elseif(v instanceofEditText) { ((EditText) v).setTypeface(tf); } elseif(v instanceofViewGroup) { changeFonts((ViewGroup) v, act); } } } } |