The font of Android app does not change with the change of the system global font size

From android4.0 onwards, the "display" of the system settings provides the option to set the font size. Take Samsung s4 as an example, select "Settings - My Device - Display - Font Size" to adjust the font of the system. This setting will directly affect the font adaptation of all sp units, so many apps become completely unrecognizable in an instant after setting the system font.
For this setting, the fontScale under Configuration is affected.

Configuration conf = getResources().getConfiguration();  
Log.e("TestTAG""fontScale=" + conf.fontScale);  

Although google recommends using sp as the unit of font, the actual development process is usually based on UIUE's design draft to convert sp (px to sp). The value of sp is not the same even at the same density. For example, on a 240dpi device, if the resolution is 480x800, this value is usually 1.5 times (scaledDensity=1.5), and if it is 480xZ (z>800), then this value may be greater than 1.5. This undoubtedly brings more difficulties and traps to the adaptation of the device. Therefore, I usually recommend using dpi as the unit of font, so that it will not be affected by this setting. But it needs to adapt to different resolutions and provide different sizes.

For individual apps that do not need to be changed according to the size of the system font, the following code can be added to the activity base class (all activities in the app should inherit from a BaseActivity class defined by ourselves). Tested and feasible.

@Override  
public Resources getResources() {  
    Resources res = super.getResources();    
    Configuration config=new Configuration();    
    config.setToDefaults();    
    res.updateConfiguration(config,res.getDisplayMetrics() );  
    return res;  
}

However, the strange thing is that if this statement is called in onCreate and onResume, the case of modifying the font and then entering after home exit cannot be solved. Need to cooperate with the attribute configChanges of Activity.

Android:configChanges="fontScale"

Another blogger provided the following solution, which has not been tested and is only for reference.

  • When I converted px to sp, I saw metrics.scaledDensity in order to see the conversion formula, so I printed this and looked at it, and then when I switched between different font sizes, this was changing, and it was adjusted to the normal display value. is 3.0. Because I use 1080P resolution, we know that px is on this screen size, 1dp = 3px, so changing the font size of the mobile phone system must be related to the property of scaledDensity. In the mobile phone settings, the value of scaledDensity in the system is changed. , thus causing the native font size in each APP to be changed. In order to keep our interface unchanged, we must get the original scaling factors of different screen sizes, so we started to look at the properties and methods in DisplayMetrics and found density. I tried to print it out and looked at it, and found that this value is fixed. I changed a 720P mobile phone and looked at it. The value was 2.0, while the previous 1080P displayed 3.0, so this is the value of saving the original scale coefficient. This is our idea. As long as we assign the value of scaledDensity to density, we can ensure that the font size in our interface will not change with external settings.
  • With the above steps, then I added the following code to the onCreate method in the Application
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    displayMetrics.scaledDensity = displayMetrics.density;
    but this is only useful when entering, if we HOME came out halfway, changed the font size, the font size displayed in our APP will still change, so finally I added the above code to the onCreate() and onRestart() methods in the base class BaseActivity of our project.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853066&siteId=291194637