Android Modify Program Font

Source: Internet
Author: User

Android offers three fonts: "Sans", "serif" and "monospace".

1. Set the font 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 "respectively, there are four kinds of display methods, followed by" Sans "," serif "," monospace "and the system default mode (using Sans by default). English fonts are different, seemingly Chinese fonts do not differ. 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 used to specify the font –> android:textsize=" 20sp "/></TABLEROW> Similar, set two TableRow in turn, 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, we copy the "*.ttf" to the past.

2) We need to set the widget to that font, which, unfortunately, cannot be done directly in the XML file and requires writing the source code.

TextView TV = (TextView) Findviewbyid (r.id.c12_custom);//Get resources from assert, get app assert, use Getaserts (), by giving the assert/ The relative path below. In actual 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 Chinese Xingkai fonts in the simulator, about 4M, but the system does not recognize the font, no display, 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 problematic, for incompatible fonts, Android can not error, but not normal display. In general, we will use the fonts provided by the system default.

For the Chinese Xingkai font, we began to use the file is a Chinese name, error, and then we will change it to full lowercase English name will not be wrong, so in the file naming needs attention.

By the way to everyone a font library, to download it,

3, some attention

The use of other fonts, will consume the program space, which is to be very careful. And these fonts sometimes don't give you exactly the text you need.

As an example, omit the way. When there is too much text, you can omit the following by ellipsis, and the ellipsis is used as a font, which can be set by using 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, in order to ensure the length of the always, Android will be filled with, in addition to a character to omit the match, the following character will be replaced by a special Unicode character, ' ZERO WIDTH no-break SPACE ' (U+feff). This character takes up any visual position, but guarantees that the string has the same length. Not all fonts support this special character and may cause some garbled behavior.

Android is supported for international languages, but we still need to be careful with custom fonts.

Android Modify Program Font

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.