Solve the problem that the UI layout is messy when the system changes the font size.

Source: Internet
Author: User

the" display "set by the system from android4.0 provides the option to set the font size. This setting directly affects the font adaptation of all sp units. Therefore, many apps instantly become invisible after setting the system font. Solution

Resources res = getresources (); configuration Config = new configuration (); config. settodefaults (); Res. updateconfiguration (config, Res. getdisplaymetrics (); although Google recommends using SP as the font unit, the actual development process usually converts Sp (PX to SP) based on the uiue design draft ). SP has different values even under the same density. For example, for devices with 1.5 DPI, if the resolution is 1.5x800, the value is usually 1.5 times (scaleddensity =), and if the resolution is X (z>), the value may be greater. This undoubtedly brings more difficulties and traps for device adaptation. Therefore, I usually recommend that you use DPI as the font unit. For some apps, you do not need to change the size of the system font, you can add the following code in the activity base class (all the activities in the app should inherit from an activity class we define): for example, contacts needs to be in COM. android. contacts. activities. transactionsafeactivity Add the following code @ override public resources getresources () {resources res = super. getresources (); configuration Config = new configuration (); config. settodefaults (); Res. updateconfiguration (config, Res. getdisplaymetrics (); Return res;} if only some interfaces are not required in the app, you can modify this method @ override public resources getresources () {If (isneedsystemresconfig ()) {return Super. getresources ();} else {resources res = super. getresources (); configuration Config = new configuration (); config. settodefaults (); Res. updateconfiguration (config, Res. getdisplaymetrics (); Return res ;}// by default, true is returned. system resources are used. If this method is not required on some interfaces, override this method and then return false in these activities; protected Boolean isneedsystemresconfig () {return true ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.