Detailed explanation of how Android dynamically adjusts application font size

preface

Why set the font size dynamically? As the project is facing middle-aged and elderly customers, the font in the project can not meet the needs of customers.

By default, the font size of Android applications changes with the font size set by the system, but you may not want your application font size to change with the system setting and want to control it yourself, such as wechat. This article briefly introduces how to realize the dynamic adjustment of application font size instead of relying on system settings

Font size change is controlled by fontscale in android.content.res.configuration.class class class. Therefore, if we want our application font size change to be controlled by ourselves instead of changing with the system, we need to modify the value set by fontscale for ourselves. Due to the problem of Android system update, you may not be able to achieve your goal through online answers. According to the author's test, we need to process from the activity level to independently control the font size change. In order to facilitate unified processing, the control logic is placed in the application, and the example pictures and codes are posted below (because my level is really low, I can't make a slider to control the font size, so in the example, two textviews are used to click to control the font size change, ha ha ~)

First, we need to let the application implement the activitylifecyclecallbacks interface and register activitylifecyclecallbacks to monitor all activities and maintain a list of activities. In the onactivitycreated method, we can monitor the creation of each activity. When an activity is created, we can obtain its resource resources, and then judge whether the configuration.fontscale value is equal to our own fontscale value (default 1.0, that is, the size has not been adjusted). If the value is inconsistent, modify it. In fact, as long as we modify the configuration.fontscale value through one activity, when all other activities obtain the configuration.fontscale value, its value is the value we modified. Through this wave of operation, we have ensured that the font size of our application does not change with the system settings.

Let's see how to dynamically adjust the application font size. Take a look at the setappfontsize method. This method traverses the activity list, obtains the resource configuration information and modifies its fontscale value. It should be noted that if you want to take effect in time, you need to call the recreate() method of the activity.

Note: the recreate () method will lead to activity reconstruction, but fortunately, calling this method will trigger the onsaveinstancestate callback. Therefore, it is strongly recommended that you save the data in this callback method to recover the data during reconstruction.

Another suggestion is: don't bury the font size setting option level too deeply. It's best to put it on the home page, because this placement will only lead to the reconstruction of the home page when adjusting the font size, which will not affect the replacement page, and will minimize the impact

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>