Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Every week will be uniformly updated here, if you like, you can follow the public account to get the latest article.

Shall not be reproduced without permission.

sequence

The use of custom fonts on Android has been a common requirement and has recently been studied in depth.

I was going to write another article about Android font modification, but there was a lot to write, so I decided to break it down into several articles (maybe five). There will be some common Fonts replacement solutions, and finally some global Fonts replacement solutions, including the latest “Fonts in XML” solution.

I look forward to your continued attention.

A, the opening

Because Android font related content is still more. Sometimes we just need to adjust the properties to meet the needs of the designer, or a backward solution (after all, there is a release time stuck), some effects can roughly meet the needs.

So this article will first introduce some basic concepts and use of some fonts native to Android.

Note that some of the built-in fonts only work with English.

Android’s default font

Android uses a font called Robote by default. Robote itself is Google’s own font format, which is used by default on both Android and Chrome operating systems and is recommended for Google’s Visual language.

To learn more about Robote, check out Google’s website.

Fonts.google.com/specimen/Ro…

Normally, Robote already offers a variety of options, such as thickness, italics, and so on. But often it doesn’t meet our needs as designers.

Let’s take a look at what attributes we need to use when we want to use some of Android’s built-in fonts.

What attributes can affect fonts

Android already provides properties and methods to modify font styles.

When you want to modify a font, you are presented with three properties, each of which has corresponding Java methods.

  • android:textStyle
  • android:typeface
  • android:fontFamily

These attributes are described in detail below.

Android 3.1: textStyle

TextStyle is mainly used to set the style of some fonts. It works for all fonts. This means that even if you replace the font, you can still use textStyle to modify its style. TextStyle itself has the normal support options | bold | italic, they are also very good understanding, is ordinary | | in bold italic.

/f-textStyle.png

As you can see, the font is not affected by the textStyle, only its style is affected.

Android 3.2: typeface

Typeface can be used to set some default font, it is an optional attribute has normal sans | | serif | monospace, etc. Normal is the same font as sans, serif is a serif font and nonospace is a constant width font.

The light says so, also cannot understand them actually, go up effect chart to be able to explain a problem quite directly.

/f-typeface.png

You can see that serif adds serif lines to the default font. Nonospace limits the width of each character to an equal width.

Equal width is easy to understand. What exactly does serif mean? Here’s an illustration from Wikipedia to illustrate the point.

/f-textserif.png

The serif is just a little tweet at the edge of the font.

Android 3.3: fontFamily

FontFamily looks like an enhancement to TypeFace, as evidenced by its optionality, which distinguishes font styles more carefully.

There are many options for fontFamily, and I won’t list them here. Note, however, that some font Settings are version limited.

For example, sans-serif-medium requires Android 5.0 to support it.

Here are a few examples to see the effect:

/f-fontfamily.png

Note that if both Typeface and fontFamily are configured, the font configured by fontFamily will be used.

Both typeFace and fontFamily can be used with typeStyle properties, except for the fact that typeFace and fontFamily conflict. In other words, fonts can be bolded or italicized.

Modify global fonts with themes

If you can convince your designers to accept the system font, you can configure the default font you want in the Theme.

In the application, configure the theme of an App with Android: Theme. Most new projects are created with @style/AppTheme. Append the font property android:fontFamily to it, and it can complete the global setting of a system font. Of course, you can configure a special font for a single TextView, which is acceptable.

/f-basetheme.png

Use custom fonts

There are times when the default fonts on Android are no longer enough for us, and we need to use some custom fonts.

As for fonts, here are two recommended websites where you can download free fonts for us to use.

fonts.google.com/

www.1001freefonts.com/

Font files, which are usually provided to us by designers, are generally in.ttf(TrueType) or.otf(OpenType) formats. The most common is.ttf format.

To load a font file, you need to use the Typeface class, which provides apis to help you load a customized font file.

The following example, for example, is a common practice.

/f-createfont.png

Typeface is not just for loading custom font files. The built-in fonts are also managed by Typeface, which is the font manager for Android.

All right, that’s it for today.

The next trailer

Typeface provides us with a very convenient Api. The next article will take you through all the details of Typeface from source code to usage.

Qr code. JPG

Like it or share it