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

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

    simple way to prevent the whole app from getting effected by system font size is to updateConfiguration using a base activity.

    //in base activity add this code.
    public  void adjustFontScale( Configuration configuration) {
    
        configuration.fontScale = (float) 1.0;
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);
    
    }
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adjustFontScale( getResources().getConfiguration());
    }
    

提交回复
热议问题