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

前端 未结 14 2088
抹茶落季
抹茶落季 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:32

    this may help. add the code in your custom Application or BaseActivity

    /**
     * 重写 getResource 方法,防止系统字体影响
     *
     * @return
     */
    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1) {
            Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }
    

    however, Resource#updateConfiguration is deplicated in API level 25, which means it will be unsupported some day in the future.

提交回复
热议问题