Ways to use custom fonts in Android

Source: Internet
Author: User

 

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);             }         }         }  }  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.