Android Study Notes (28): font

Source: Internet
Author: User

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
For Android: text = "Hello, world!
Hello, there are four display modes: "sans", "serif", "monospace", and system default mode (SANS by default ). English fonts available
Differences, it seems that there is no difference 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 new TTF file to the assets/fonts/directory. For example, we copied "*. TTF.

2) We need to set the widget to this font. Unfortunately, the widget cannot be directly carried out in the XML file and the source code needs to be compiled.

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 the support for special Chinese fonts. For incompatible fonts, no error is reported for Android, but it cannot be properly displayed. I
In general, we will 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.

3. Notes

Using other font libraries will consume the space of the program. 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 use
Android: ellipsize attribute. To use the omit function, make sure that the font has a ellipsis. In addition, Android performs
Fill processing, except for 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 some garbled characters
Image.

Android supports international languages, but we still need to be careful with the M font.

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.