Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to prevent the influence of system font change and display size change

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Android to prevent system font changes, display size changes", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use Android to prevent system font changes and display size changes."

Catalogue

1. When the font of the system becomes larger

2. Solution

3. When the display size becomes larger

4. Solution

4.1 tool class

1. When the font of the system becomes larger

Activity

Fragment

When the system font is enlarged, the text set to sp is also zoomed in, and the text set to dp is saved.

How to modify what is set to sp?

2. Solution

Use dp units

Set getResources () in Activity

Can be handled uniformly in BaseActivity

@ Overridepublic Resources getResources () {Resources resources = super.getResources (); if (resources! = null & & resources.getConfiguration (). FontScale! = 1.0f) {android.content.res.Configuration configuration = resources.getConfiguration (); configuration.fontScale = 1.0f; resources.updateConfiguration (configuration, resources.getDisplayMetrics ());} return resources;} 3, when the display size becomes larger

4. Solution 4.1 tool class public class DisplayUtil {private static final String TAG = DisplayUtil.class.getSimpleName (); / * * when the system is set to "display size", the original UI style remains unchanged: *

* 1. When you adjust the display size of the mobile phone system, the corresponding dpi will become larger [dp = (dpi/160) * px], and the dp will become larger, so the corresponding UI layout will become larger. * 2. When you adjust the "resolution" of the mobile phone system, the corresponding dpi will become smaller [for example, from 480 to > 320]. If you use technical means to keep the dpi unchanged at this time, the same dp will take up more px, so the UI layout will be larger. * * @ param context * / public static void setDefaultDisplay (Context context) {if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {Configuration origConfig = context.getResources () .getConfiguration (); / / get the default densityDpi [Note 1] origConfig.densityDpi = getDefaultDisplayDensity () when the phone leaves the factory. Context.getResources () .updateConfiguration (origConfig, context.getResources (). GetDisplayMetrics ());} public static int getDefaultDisplayDensity () {try {Class clazz = Class.forName ("android.view.WindowManagerGlobal"); Method method = clazz.getMethod ("getWindowManagerService"); method.setAccessible (true); Object iwm = method.invoke (clazz) Method getInitialDisplayDensity = iwm.getClass (). GetMethod ("getInitialDisplayDensity", int.class); getInitialDisplayDensity.setAccessible (true); Object densityDpi = getInitialDisplayDensity.invoke (iwm, Display.DEFAULT_DISPLAY); return (int) densityDpi;} catch (Exception e) {e.printStackTrace (); return-1;}

Call DisplayUtil.setDefaultDisplay (this) before Activity#setContentView ()

At this point, I believe you have a deeper understanding of "how to use Android to prevent system font changes and display size changes". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report