android 解决系统字体大小设置引起的布局混乱问题(两种方法)

在手机系统设置中,若是修改了字体大小,会影响 app 内字体显示,导致布局混乱不齐。有两种方法,一般推荐第二种方法。


android 解决系统字体大小设置引起的布局混乱问题(两种方法)_第1张图片
字体设置

方法一:

字体大小单位使用 dp,而不是使用 sp。注意在 java 代码中需要用 dp 方式显示。默认是使用 sp。

    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30); // 注意!!是TypedValue.COMPLEX_UNIT_DIP

为什么呢?我们刚学 android 时,不是说字体大小用 sp,布局大小用 dp 吗?
别急,下面看看字体设置的源码:

    public static float applyDimension(int unit, float value,
                                       DisplayMetrics metrics){
        switch (unit) {
        case COMPLEX_UNIT_PX:
            return value;
        case COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f/72);
        case COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f/25.4f);
        }
        return 0;
    }

可以发现,dp 和 sp 的区别就是densityscaledDensity。下面再看看两者的区别:

    /**
     * The logical density of the display.  This is a scaling factor for the
     * Density Independent Pixel unit, where one DIP is one pixel on an
     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), 
     * providing the baseline of the system's display. Thus on a 160dpi screen 
     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
     *  
     * 

This value does not exactly follow the real screen size (as given by * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of * the overall UI in steps based on gross changes in the display dpi. For * example, a 240x320 screen will have a density of 1 even if its width is * 1.8", 1.3", etc. However, if the screen resolution is increased to * 320x480 but the screen size remained 1.5"x2" then the density would be * increased (probably to 1.5). * * @see #DENSITY_DEFAULT */ public float density; /** * A scaling factor for fonts displayed on the display. This is the same * as {@link #density}, except that it may be adjusted in smaller * increments at runtime based on a user preference for the font size. */ public float scaledDensity;

简单说,就是density不会受到用户配置的影响,而scaledDensity除了会受到用户配置的影响,其它方面是跟density一致的。
终于真相大白!因此使用 sp 单位时字体大小会受到用户配置系统字体的影响。

方法二:(推荐)

在 activity 内重写getResources方法,如此在 xml 和 java 代码使用 sp 字体单位都是正常的。一般在BaseActivity内重写。

    /**
     * 设置 app 字体不随系统字体设置改变
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res != null) {
            Configuration config = res.getConfiguration();
            if (config != null && config.fontScale != 1.0f) {
                config.fontScale = 1.0f;
                res.updateConfiguration(config, res.getDisplayMetrics());
            }
        }
        return res;
    }

注意!网上很多地方写着使用config.setToDefaults();,实际上除了影响 app 内字体大小,还会影响很多地方的属性值。我们来看一下源码:

    /**
     * Set this object to the system defaults.
     */
    public void setToDefaults() {
        fontScale = 1;
        mcc = mnc = 0;
        mLocaleList = LocaleList.getEmptyLocaleList();
        locale = null;
        userSetLocale = false;
        touchscreen = TOUCHSCREEN_UNDEFINED;
        keyboard = KEYBOARD_UNDEFINED;
        keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
        hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
        navigation = NAVIGATION_UNDEFINED;
        navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
        orientation = ORIENTATION_UNDEFINED;
        screenLayout = SCREENLAYOUT_UNDEFINED;
        uiMode = UI_MODE_TYPE_UNDEFINED;
        screenWidthDp = compatScreenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
        screenHeightDp = compatScreenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
        smallestScreenWidthDp = compatSmallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
        densityDpi = DENSITY_DPI_UNDEFINED;
        seq = 0;
    }

因此,我们只需要把fontScale属性值设置成默认值1即可。一开始我也没考虑到这个点,感谢sollian在评论留言的提醒。

总结

  1. 若是想要全局控制字体是否受系统设置影响,推荐使用方法二重写getResources方法。
  2. 若是部分字体不需要受系统设置影响,部分字体需要受系统设置影响,推荐使用方法一。

你可能感兴趣的:(android 解决系统字体大小设置引起的布局混乱问题(两种方法))