在BaseActivity或所需要的Activity中重写 getResources()
override fun getResources(): Resources {
val resources = super.getResources()
val configContext = createConfigurationContext(resources.configuration)
return configContext.resources.apply {
configuration.fontScale = 1.0f
displayMetrics.scaledDensity = displayMetrics.density * configuration.fontScale
}
}
override fun attachBaseContext(newBase: Context) {
val fontScale = SPUtils.getFloat("FONT_SCALE")
ELog.d(TAG, "attachBaseContext: fontScale=$fontScale")
val config = newBase.resources.configuration
config.fontScale = fontScale
val context = newBase.createConfigurationContext(config)
super.attachBaseContext(context)
}