直接上代码工具类 简单明了
1 /** 2 * Author : monkey0928 3 * E-mail : hanbao@xwtec.c 4 * Date : 2020/6/29 17:54 5 * DESCRIBE :禁用 7.0(23)以上字体大小和显示大小随系统变化 6 */ 7 public final class DispUtility { 8 9 /** 10 * 禁用7.0(23)以上显示大小改变和文字大小 11 */ 12 public static Resources disabledDisplayDpiChange(Resources res) { 13 Configuration newConfig = res.getConfiguration(); 14 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 15 //字体非默认值 16 if (res.getConfiguration().fontScale != 1) { 17 newConfig.fontScale = 1; 18 } 19 newConfig.densityDpi = getDefaultDisplayDensity(); 20 res.updateConfiguration(newConfig, res.getDisplayMetrics()); 21 } else { 22 //字体非默认值 23 if (res.getConfiguration().fontScale != 1) { 24 newConfig.fontScale = 1;//设置默认 25 res.updateConfiguration(newConfig, res.getDisplayMetrics()); 26 } 27 } 28 return res; 29 } 30 31 /** 32 * 获取手机出厂时默认的densityDpi 33 */ 34 public static int getDefaultDisplayDensity() { 35 try { 36 Class aClass = Class.forName("android.view.WindowManagerGlobal"); 37 Method method = aClass.getMethod("getWindowManagerService"); 38 method.setAccessible(true); 39 Object iwm = method.invoke(aClass); 40 Method getInitialDisplayDensity = iwm.getClass().getMethod("getInitialDisplayDensity", int.class); 41 getInitialDisplayDensity.setAccessible(true); 42 Object densityDpi = getInitialDisplayDensity.invoke(iwm, Display.DEFAULT_DISPLAY); 43 return (int) densityDpi; 44 } catch (Exception e) { 45 e.printStackTrace(); 46 return -1; 47 } 48 } 49 50 }
接着在代码当中使用即可(BaseActivity或Application)
1 DispUtility.disabledDisplayDpiChange(this.getResources());