Android offers three different fonts: "
Sans”,“
Serifand
monospace”。
1. Set fonts in the Android XML file
You can use Android:typeface, such as android:typeface= "monospace". In this example we are in the activity of android:text= "Hello, world! Hello "Four different display methods, in order," Sans "," serif "," monospace "and the system default mode (the test by default using Sans). There are differences in English fonts, there seems to be no difference in Chinese fonts.
The XML file is as follows:
<?xml version= "1.0″encoding=" utf-8″?>
<tablelayout ... android:stretchcolumns = "1″>
<TableRow>
<textviewandroid: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>
... ... Similar, in turn, set two TableRow, respectively, sans modified 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 new font TTF file to the assets/fonts/directory, for example, we will "*.ttf" Copy the past.
2 We need to set the widget to this font, more unfortunately, not directly in the XML file, you need to write the source code.
TextView TV = (TextView) Findviewbyid (R.id.c12_custom);
Get the resources from the Assert, get the app assert, use Getaserts (), by giving the relative path below the assert/. In practical use, the font library may exist on the SD card, you can use CreateFromFile () to replace the Createfromasset.
typeface face = Typeface.createfromasset (Getassets (), "Fonts/timesi.ttf");
Tv.settypeface (face);
I have imported the Chinese Xingkai font in the simulator, about 4M, but the system cannot recognize the font, does not display it, and then try to use the English font Timesi.ttf, normal. Therefore, Android is not compatible with all TTF fonts, especially in the Chinese special font support will be a problem, for incompatible fonts, Android does not give an error, but not normal display. In general, we use the fonts provided by the system defaults.
For the Chinese Xingkai font, we first use the file is the Chinese name, there is an error, then we will change it to all lowercase English name will not be wrong, so in the name of the file need to pay attention.
The
3, some attention
uses other fonts, and consumes the space of the program, which is very important. And these fonts sometimes do not fully provide the text you need.
For example, omit the way. When there is too much text, you can omit the following from the ellipsis by using the "..." as a font, which can be set by the Android:ellipsize property. If we need to use the ellipsis feature, we need to make sure that the font has an ellipsis. In addition, to ensure that the length of the Android will be filled with, in addition to replacing one character with an ellipsis, the following characters will be replaced with a special Unicode character, ' ZERO WIDTH no-break space ' (U+feff). This character takes up any visible position, but it guarantees that the string has the same length. Not all fonts support this particular character, and may cause some garbled behavior.
Android is a support for international languages, but we still need to be careful with custom fonts.