How to resolve the use of custom fonts in Android

Source: Internet
Author: User

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

2. Other fonts can be introduced in Android.

Copy codeThe Code is as follows: <? 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">
</TextView>
<! -- Use the default sans font -->

<TextView
Android: id = "@ + id/sans"
Android: text = "Hello, World"
Android: textSize = "20sp"
Android: typeface = "sans">
</TextView>
</TableRow>

<TableRow>

<TextView
Android: layout_marginRight = "4px"
Android: text = "serif :"
Android: textSize = "20sp">
</TextView>
<! -- Use the default serifs font -->

<TextView
Android: id = "@ + id/serif"
Android: text = "Hello, World"
Android: textSize = "20sp"
Android: typeface = "serif">
</TextView>
</TableRow>

<TableRow>

<TextView
Android: layout_marginRight = "4px"
Android: text = "monospace :"
Android: textSize = "20sp">
</TextView>
<! -- Use the default monospace font -->

<TextView
Android: id = "@ + id/monospace"
Android: text = "Hello, World"
Android: textSize = "20sp"
Android: typeface = "monospace">
</TextView>
</TableRow>
<! -- No font is set here. We will set it in Java code -->

<TableRow>

<TextView
Android: layout_marginRight = "4px"
Android: text = "custom :"
Android: textSize = "20sp">
</TextView>

<TextView
Android: id = "@ + id/custom"
Android: text = "Hello, World"
Android: textSize = "20sp">
</TextView>
</TableRow>

</TableLayout>

Copy codeThe Code is as follows: // 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:
Copy codeThe Code is as follows: 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 );
}
}

}
}

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.