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());
  • 相关阅读:
    软件编程含有中文的编码问题
    iostream与iostream.h
    C++变量的定义
    c++标准线程库
    C++单例模式
    C++,类中重载函数的调用,类中模板函数定义与调用。
    c++ stl
    C++ static调用
    openssl基本概念
    C语言malloc(0)情况分析与malloc字节对齐
  • 原文地址:https://www.cnblogs.com/monkey0928/p/13353339.html
Copyright © 2011-2022 走看看