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