How to use custom fonts in Android development

Source: Internet
Author: User

1, the Android system defaults to support three kinds of fonts, respectively: "Sans", "serif", "monospace

2, in Android can introduce other fonts.

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" >

Get TextView Control Object

TextView TextView = (TextView) Findviewbyid (R.id.custom);

Save the font file in the assets/fonts/directory, www.linuxidc.com create the 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 on the entire 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 wonderful tutorials, please pay attention to the Triple graphics tutorial channel,

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.