Font size of TextView in Android application changes on changing font size from native settings

前端 未结 14 2140
抹茶落季
抹茶落季 2020-12-04 07:47

I want to specify my own text size in my application, but I am having a problem doing this.

When I change the font size in the device settings, the font size of my

14条回答
  •  情书的邮戳
    2020-12-04 08:36

    this solutions is with Kotlin and without using the deprecated function resources.updateConfiguration

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            adjustFontScale(resources.configuration)
        }
    
        private fun adjustFontScale(configuration: Configuration?) {
            configuration?.let {
                it.fontScale = 1.0F
                val metrics: DisplayMetrics = resources.displayMetrics
                val wm: WindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
                wm.defaultDisplay.getMetrics(metrics)
                metrics.scaledDensity = configuration.fontScale * metrics.density
    
                baseContext.applicationContext.createConfigurationContext(it)
                baseContext.resources.displayMetrics.setTo(metrics)
    
            }
        }
    

    Observation: this is the same solution as the above but udpated with Kotlin

提交回复
热议问题