The use of fonts and precautions in Android programming _android

Source: Internet
Author: User
Support for TextView fonts is very limited under the Android operating system, and by default TextView typeface attributes support "Sans", "serif", "monospace" three fonts, and if no fonts are specified, the system lacks the provincial capital. Sans the font to display as text. But these three fonts only support English, that is to say, as long as you display the text is Chinese, regardless of which one of the three fonts you choose, the display effect is the same. To this end, Google has provided another type of font support way: Font. Android's support for fonts is limited in the 2.2 version, with Android 2.3.3 beginning to support most of the fonts. This means that the code you write with the SDK version for Android 2.2 will only work with the Android 2.3.3 system and its newer version, and if you run the code on the Android 2.2 system, you won't see what you're looking for, instead A blank.

Here is a look at the code implementation process, before the implementation of code implementation, we first look at the different font of the display effect, the first figure in the following figure: Simkai.ttf Simplified Chinese-italics-general, second picture: Msyh.ttf simplified-Microsoft ya Black-General.


Copy Code code as follows:

Package cn.sunzn.fonts;
Import android.app.Activity;
Import Android.graphics.Typeface;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.widget.TextView;
public class Mainactivity extends activity {
Private TextView Tv_title;
Private TextView Tv_author;
Private TextView tv_line1;
Private TextView Tv_line2;
Private TextView Tv_line3;
Private TextView tv_line4;
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
/**
* Create the Fonts folder in the assets directory and put it in the font file (**.ttf) to use and provide a relative path to
* Createfromasset (Assetmanager Mgr, String Path) to create typeface objects, and then pass
* Textview.settypeface (typeface tf) specifies the font to display text.
*/
Typeface face = Typeface.createfromasset (Getassets (), "Fonts/simfang." TTF ");
Tv_title = (TextView) Findviewbyid (r.id.tv_title);
Tv_title.settypeface (face);
Tv_author = (TextView) Findviewbyid (R.id.tv_author);
Tv_author.settypeface (face);
Tv_line1 = (TextView) Findviewbyid (R.ID.TV_LINE1);
Tv_line1.settypeface (face);
Tv_line2 = (TextView) Findviewbyid (r.id.tv_line2);
Tv_line2.settypeface (face);
Tv_line3 = (TextView) Findviewbyid (R.ID.TV_LINE3);
Tv_line3.settypeface (face);
Tv_line4 = (TextView) Findviewbyid (r.id.tv_line4);
Tv_line4.settypeface (face);
}
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}

Finally, you need to add that while this is the way to achieve the desired display, developers need to consider the performance consumption and operating environment as well as the style of their APP before making the corresponding choices before using this approach.

There are several reasons for this
The size of the font file: Because the size of the font is a few megabytes, a few 10 trillion, file packaging when the *.ttf format of the file compression rate is very low, so will virtually increase the volume of the APP.

• Operating Environment Considerations: If your application is oriented to a high version of the operating environment, consider using this method, if the user's operating environment is Android 2.2 need to give up the method.

• Application Style Considerations: If your application is for a high version of the operating environment, and you use the specified font in your application, you can consider using the method, if only for the application of a few words in the display effect of the use of the font, it will outweigh the gains.

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.