Font settings for Android apps

Source: Internet
Author: User

Android system comes with the font settings, these settings are the way the font is displayed, such as bold, italic, underline, font size, etc., but for the font itself, such as set to italic, such as the script does not seem. Android system has only one default, if you need to modify the font, or reference your favorite font, then you need to set or modify it yourself.

Modify the font of the system can be downloaded to modify the font of the app, but if the developers want to use their own characters in their own app, you can do as described below (here is just one way, I believe, there should be a lot of ways)

• Copy the TTF file that contains the custom font to the project assets/fonts/directory

The format of the general font is TTF. In the C:\WINDOWS\Fonts directory, there are fonts provided by the WINDOWS system that you can use. You can also make your own fonts through the Coreldraw+fontcreator software.

• Make the following changes in the source code:

TextView TV = (TextView) Findviewbyid (R.id.c12_custom);

Get the resources from the Assert, get the app's assert, use Getaserts (), by giving the relative path below the assert/. 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);

The settings font cannot be directly in the XML file, and it needs to be done through the source code.

In these two steps, you can change the font of the TextView object TV to its own defined font Timesi.ttf

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.

  

Some attention

The font is generally very large, because it will include a lot of character sets, the use of other fonts, which will increase the volume of the program. You can use the Fontcreator software to crop the font size to make it smaller. If the font size is larger, you can consider putting it on sdcard, using CreateFromFile to get the typeface object

Some fonts may not include the characters we need, for example, in order to save the size of the font, the Chinese characters will be canceled, so if you want to use this font to display Chinese words, it must be unsuccessful.

The source code that appears in the program is explained here.

Modifying a font is called the Settypeface method. The parameter of this method is a typeface class object

Typeface is a font class

This class is relatively simple, listing its member methods (these are static methods, returning the typeface object, which can be used directly as Settypeface parameters):

static Typeface Create (Typeface family, int style)//static method, parameter one for the font type here is the static definition of Typeface, such as the song body, parameter two styles, such as bold, italic

static Typeface Create (string familyname, int style)//static method, parameter one is the font name of the string, parameter two for style ibid, here we recommend using the method above.

static Typeface Createfromasset (Assetmanager Mgr, String path)//static method, parameter one is the Assetmanager object, mainly used to remove the font from the APK's assets folder, Parameter two is relative to the path of the plug-in font file in the Assets folder under Android engineering.

static Typeface createfromfile (file path)//static method, a font is constructed from the filesystem, where the parameter can be a font file in SDcard

static Typeface CreateFromFile (String path)//static method, constructs the font from the specified path

static Typeface defaultfromstyle (int style)//static method, returns the default font style

Constants of this class are statically defined, first for the font type name

Typeface DEFAULT

Typeface Default_bold

Typeface monospace

Typeface Sans_serif

Typeface SERIF

Font style name

int BOLD

int Bold_italic

int ITALIC

int NORMAL

These variables can all be known by name

Here is a picture of the example of the method used in the application you wrote (you can use the screenshot, this time)

Font settings for Android apps

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.