How to use custom fonts in Android Development

Source: Internet
Author: User

How to use custom fonts in Android Development

1. The Android system supports three fonts by default: "sans", "serif", and "monospace ".

2. Other fonts can be introduced in Android.

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

// Obtain the TextView control object

TextView textView = (TextView) findViewById (R. id. custom );

// Save the font file to the assets/fonts/directory and create a Typeface object at www.linuxidc.com

Typeface typeFace = Typeface. createFromAsset (getAssets (), "fonts/DroidSansThai. ttf ");

// Apply the font

TextView. setTypeface (typeFace );

To apply a Custom font to all controls on 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 );

}

}

}

}

Note: For more exciting tutorials, please follow the help houseGraphic tutorialChannel,

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.