Usage of font library in Android programming and precautions

Source: Internet
Author: User

The TextView font support is limited in the Android operating system. By default, the typeface attribute of TextView supports three fonts: "Sans", "serif", and "monospace, if no font is specified, the system uses "Sans" as the text display font by default. However, these three fonts only support English. That is to say, as long as you display Chinese characters, the display effect is the same no matter which one of the three fonts you choose. To this end, Google provides another font support method: font. In Android 2.2, Android has limited support for font libraries. Since Android 2.3.3, Android has been able to support most font libraries. This means that the font code you write on the platform where the SDK version is Android 2.2 takes effect only in Android 2.3.3 and later versions; if you run the code on the Android 2.2 system, you will not see what you expected, instead, it is blank.

Let's take a look at the code implementation process. before implementing the Code, Let's first look at the display effects of different font libraries. The first figure is SIMKAI. TTF simplified Chinese characters-Simplified body-regular and the second figure: MSYH. TTF Simplified Chinese Text--regular.

Copy codeThe Code is 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 a fonts folder under the assets Directory, put the fonts file (**. ttf) to be used, and provide the relative path
* CreateFromAsset (AssetManager mgr, String path) to create a Typeface object.
* TextView. setTypeface (Typeface tf) is used to specify the font for text display.
*/
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, we need to add a note: although this method can achieve the desired display effect, however, before using this method, developers still need to consider the performance consumption, runtime environment, and APP style, and then make corresponding choices.

The reasons are as follows::
• Font size: Because the font size is less than a few megabytes, the font size is more than dozens of megabytes *. the compression ratio of files in ttf format is very low, so the size of the APP is virtually increased.

• Runtime environment considerations: If your application is targeting a later version of the runtime environment, you can consider using this method. If your runtime environment is Android 2.2, You need to discard this method.

• Application style considerations: If your application is oriented to a high-version runtime environment and the specified font is used in all applications, you can use this method; if you use a font for the display effect of a certain number of words in the application, the loss will be worth the candle.

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.