Modify the default font globally, which can also be done through reflection

Modify the default font globally, which can also be done through reflection

[[205199]]

sequence

Using custom fonts on Android is a common requirement, and we have done some in-depth research on this recently.

As usual, I'm going to write another article about font modification on Android, but I found that there is a lot of content, so I decided to split it up and explain it in several articles (maybe five articles). It will mainly introduce some commonly used font replacement solutions, and finally some global replacement solutions, and of course it will also include the "Fonts in XML" solution of ***.

Looking forward to your continued attention.

This is the fourth article in this series. If you are interested, you can take a look at the articles that have been published before.

Android font modification overview | opening chapter

Modifying fonts requires understanding all the details of Typeface

A simple and crude way to modify the font

1. Introduction

This article still belongs to the series of Android font modification. At the beginning of this series, some convenient global modification solutions will be introduced. The later solutions may be better, but they may not be the most suitable for your current project.

What I will introduce today is one of them. I will use reflection to modify a font in Typeface to achieve the purpose of global replacement.

2. Ideas for replacing the default font

Many of the preparatory knowledge points in this article should have been explained clearly in previous articles. If you are interested, you can read the full article, "If you want to modify Android fonts, you need to understand Typeface first!!!".

In order to ensure the logical completeness, let me briefly talk about the idea here.

2.1 Modify a default font of Typeface

First of all, it should be made clear that in Android, all font manipulation actions will use the Typeface class. Some default system fonts will also be initialized when Typeface is loaded, because these steps are completed in its static code block.

These fonts are defined as static final, so generally we cannot modify them.

However, we can use reflection to modify constants marked as static final, which will be described in detail later.

In other words, we only need to replace a default font during initialization, and then mark the default font as this font in the Theme to achieve the purpose of replacement.

2.2 Default font in TextView

In the TextView constructor, the method for setting the font is setTypefaceFromAttrs(). The following is the method signature of this method.

In the parameters of this method, if familyName is null, the corresponding font will be set according to the passed typefaceIndex and passed to the setTypeface() method.

Let's take a look at the places where these parameters are obtained in the TextView constructor. In the default case where nothing is set, familyName is null and typefaceIndex is -1. These two parameters will first read the attributes from TextAppearance and then from the XML attributes set by TextView itself, and the latter will overwrite the former.

However, typefaceIndex has some other operations. For example, when inputType is password, it will be forcibly changed to MONOSPACE.

Finally, the processed structure is passed to the setTypefaceFromAttrs() method.

Through these details, we can understand that in some cases, it is possible to ensure that TextView uses one of our default fonts loaded into Typeface.

The conditions are:

  1. fontFamily == null && typefaceIndex != -1

2.3 In Theme, change the font to the default font style

For some default font styles, you can set them directly in the Theme. Its priority is lower than the font style set for TextView in the page layout xml. However, if it is not set, the setting in the Theme will take effect.

There is nothing much to say about this. The theme I use here is AppTheme, so I just need to modify android:typeface in it.

3. Modify fonts through reflection

At this point, the basic concepts have been explained clearly, so let's start writing actual code to replace the font.

3.1 Modify Theme

In the App's theme, change android:typeface to serif.

Note that we randomly selected a default font here. You can also use monospace as long as it is consistent with the font we replace later.

Of course, it is not recommended to use monospace here, because TextView itself has some logic that will set typefaceIndex to monospace, so it is better not to replace it.

3.2 Modify Typeface font through reflection

In Typeface, there are some default fonts marked as static final. Since serif was set in the Theme in the previous step, we just need to replace it here.

The complete method is very simple, just get Typeface.SERIF through reflection, and then use reflection to modify it to the font we need.

Because the value of static final is modified here, the setAccessible() method needs to be called additionally, which will modify the overide in AccessibleObject to true. This mark means turning off the security check for overwriting this field, so that we can replace the static final field.

3.3 At the entrance, call the replacement method

The next step is clear. We only need to call the changeDefaultFont() method when the App starts.

Here you can call it directly in the Application.onCreate() method.

3.4 Verify the operation results

This is nothing, just write a Demo and use TextView normally.

[This article is an original article by 51CTO columnist "Zhang Yang". Please contact the author through WeChat official account to obtain authorization for reprinting]

Click here to read more articles by this author

<<:  How to solve the multi-body problem in deep learning

>>:  How to use Vim on Android to improve development efficiency

Recommend

Overseas video marketing promotion: YouTube influencer marketing guide!

YouTube is quickly becoming one of the most popul...

iPod creator: I can save Google Glass

[[142104]] On July 26, despite the huge attention...

China's bumper harvest: Jilin's colors painted on the black soil

In the golden autumn season, the rice in Jilin Pr...

Steve Jobs was also very conflicted about whether to bring iTunes to PC

Perhaps many PC users will complain about various...

Recommend Chengdu tea tasting 90 minutes unlimited studio Bashi board

Recommend Chengdu tea tasting 90 minutes unlimite...

Summarize 9 KPI indicators to measure whether an APP is successful

What is the most important metric for measuring m...

FastQuery: a framework for fast data operations

FastQuery fast operation data layer framework Fas...

Only 3 tricks are needed to attract traffic and promote Xiaohongshu!

Xiaohongshu App is hailed as a magic tool for you...