zoukankan      html  css  js  c++  java
  • android 禁用 7.0(23)以上字体大小和显示大小随系统变化

    直接上代码工具类  简单明了

     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());
  • 相关阅读:
    将word转化为swf 进行如同百度文库的般阅读
    最大子数组问题——编程珠玑第八章
    为什么静态成员必须在类外初始化
    C++初始化列表
    异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接
    [PLL][PM]锁相环模拟相位解调
    insertion sort
    SRM 581 D2 L2:SurveillanceSystem,重叠度
    JQuery(下)
    Ajax
  • 原文地址:https://www.cnblogs.com/monkey0928/p/13353339.html
Copyright © 2011-2022 走看看