Android Custom font

Source: Internet
Author: User

In the Android system, only three fonts are provided: "sans", "serif", and "monospace ".

Android: Typeface

Android: Typeface attributes use them:

<Tablelayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: stretchcolumns = "1">
<Tablerow>
<Textview
Android: text = "sans :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/sans"
Android: text = "Hello, world! "
Android: Typeface = "sans"
Android: textsize = "20sp"
/>
</Tablerow>
<Tablerow>
<Textview
Android: text = "serif :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/serif"
Android: text = "Hello, world! "
Android: Typeface = "serif"
Android: textsize = "20sp"
/>
</Tablerow>
<Tablerow>
<Textview
Android: text = "monospace :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/monospace"
Android: text = "Hello, world! "
Android: Typeface = "monospace"
Android: textsize = "20sp"
/>
</Tablerow>
<Tablerow>
<Textview
Android: text = "custom :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/custom"
Android: text = "Hello, world! "
Android: textsize = "20sp"
/>
</Tablerow>
</Tablelayout>

<Tablelayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: stretchcolumns = "1">
<Tablerow>
<Textview
Android: text = "sans :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/sans"
Android: text = "Hello, world! "
Android: Typeface = "sans"
Android: textsize = "20sp"
/>
</Tablerow>
<Tablerow>
<Textview
Android: text = "serif :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/serif"
Android: text = "Hello, world! "
Android: Typeface = "serif"
Android: textsize = "20sp"
/>
</Tablerow>
<Tablerow>
<Textview
Android: text = "monospace :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/monospace"
Android: text = "Hello, world! "
Android: Typeface = "monospace"
Android: textsize = "20sp"
/>
</Tablerow>
<Tablerow>
<Textview
Android: text = "custom :"
Android: layout_marginright = "4px"
Android: textsize = "20sp"
/>
<Textview
Android: Id = "@ + ID/custom"
Android: text = "Hello, world! "
Android: textsize = "20sp"
/>
</Tablerow>
</Tablelayout>

Sometimes, the fonts provided by the system cannot meet our special requirements. At this time, we need to reference other fonts. We can put the downloaded font files under the Assets Directory. the Custom font file cannot be read using XML code, but Java code should be used:


Public class test extends activity {
@ Override
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. Main );

Textview TV = (textview) findviewbyid (R. Id. Custom );
Typeface face = typeface. createfromasset (getassets (),
"Fonts/handmadetypewriter. TTF"); // you can also use other static methods to obtain

TV. settypeface (FACE );
}
}

Public class test extends activity {
@ Override
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. Main );

Textview TV = (textview) findviewbyid (R. Id. Custom );
Typeface face = typeface. createfromasset (getassets (),
"Fonts/handmadetypewriter. TTF"); // you can also use other static methods to obtain

TV. settypeface (FACE );
}
}

Download the TTF file: windows.zip | mac.zip (select the corresponding system)

Note: If the font you downloaded is not supported by Android, the system uses the default font "sans ". Because the application of custom fonts requires additional space, not all of them require custom fonts.

Reprinted: http://www.androidmi.com/Androidkaifa/jinjie/201011/1632.html

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.