override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.fontScale != 1.0f) {
//刷新
resources
}
super.onConfigurationChanged(newConfig)
}
override fun getResources(): Resources {
val res = super.getResources()
if (res != null && res.configuration.fontScale != 1.0f) {
res.configuration.fontScale = 1.0f //恢复默认的[字体大小]
res.configuration.densityDpi = defaultDisplayDensity //恢复默认的[显示大小]
res.updateConfiguration(res.configuration, res.displayMetrics)
}
return res
}
private val defaultDisplayDensity by lazy {
try {
val clazz = Class.forName("android.view.WindowManagerGlobal")
val method = clazz.getMethod("getWindowManagerService")
method.isAccessible = true
val iwm = method.invoke(clazz)
val getInitialDisplayDensity =iwm.javaClass.getMethod("getInitialDisplayDensity", Int::class.java)
getInitialDisplayDensity.isAccessible = true
val densityDpi = getInitialDisplayDensity.invoke(iwm, Display.DEFAULT_DISPLAY);
densityDpi as Int
} catch (e: Exception) {
e.printStackTrace()
-1
}
}