Android provides three fonts: "sans", "serif", and "monospace ".
1. Set the font in the android XML file
Android: Typeface can be used, for example, Android: Typeface = "monospace ". In this example, Android: text = "Hello, world! Hello, there are four display modes: "sans", "serif", "monospace", and system default mode (SANS by default ). There are differences in English fonts. It seems that there are no differences in Chinese fonts. The XML file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Tablelayout... ... Android: stretchcolumns = "1">
<Tablerow>
<Textview Android: text = "sans :"
Android: layout_marginright = "4px"
Android: textsize = "20sp" />
<Textview Android: text = "Hello, world! Hello"
Android: Typeface = "Sans" <! -Android: typeface is used to specify the font.->
Android: textsize = "20sp" />
</Tablerow>
... ... Similarly, set two tablerow in sequence and change sans to Serif, monospace... ...
<Tablerow>
<Textview Android: text = "custom :".... />
<Textview Android: Id = "@ + ID/c12_custom"
Android: text = "Hello, world! Hello"
Android: textsize = "20sp"/>
</Tablerow>
</Tablelayout>
2. Use other fonts
1) copy the TTF file of the new font to the assets/fonts/directory, for example, TTF "copies the past.
2) We need to set the widget to this font. Unfortunately, the widget cannot be directly carried out in the XML file and needs to be written.Source code.
Textview TV = (textview) findviewbyid (R. Id. c12_custom );
// Obtain resources from assert to obtain the assert of the app. getaserts () is used to obtain the relative path under assert. In actual use, the library may exist on the SD card. You can use createfromfile () to replace createfromasset.
Typeface face = typeface. Createfromasset (Getassets () , "Fonts/timesi. TTF" );
TV. Settypeface (FACE );
In the simulator, I have successively imported a Chinese Character Font, which is about 4 MB. However, the system cannot identify the font and does not display it. Then I tried to use the English text timesi. TTF, which is normal. Therefore, Android is not compatible with all TTF fonts, especially in Chinese special fonts. For incompatible fonts, no error is reported for Android, but it cannot be properly displayed. Generally, we use the default font provided by the system.
For the Chinese Character Font, we used the Chinese name in the first place, and an error was reported. Then we changed it to a full-lowercase English name, so there is no error, so pay attention to the file name.
By the way, let's download a Chinese library,
3. Notes
Using other font libraries will consumeProgramThis is very important. These fonts sometimes do not provide all the text you need.
For example, the method is omitted. When there are too many texts, You can omit the following content by using the ellipsis (...). As a font, you can set it through the Android: ellipsize attribute. To use the omit function, make sure that the font has a ellipsis. In addition, to ensure that the length is always the same, Android will perform filling. In addition to replacing a character with an omitted character, the subsequent characters will be replaced with a special UNICODE character, 'Zero width no-break space '(U + feff ). This character occupies any visible position, but ensures that the string has the same length. Not all fonts support this special character and may cause garbled characters.
Android supports international languages, but we still need to be careful with the M font.