How to set font scale in my own android application?

后端 未结 3 432
名媛妹妹
名媛妹妹 2020-12-30 15:10

I want to allow user select font size.

Spare Parts do provide the feature to do that,

ActivityManagerNative.getDefault().updateConfiguratio

3条回答
  •  离开以前
    2020-12-30 15:53

    The code below will allow to change for the application only the font scale. On second line, the code "configuration.fontScale=(float) 1;" will have to chabge to meet your needs. The defaults are scaling with 0.15 step, having default 1.

    Place it before calling setContentView(R.layout.yourlayout)

    Configuration configuration = getResources().getConfiguration();
    configuration.fontScale=(float) 1; //0.85 small size, 1 normal size, 1,15 big etc
    
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = configuration.fontScale * metrics.density;
    getBaseContext().getResources().updateConfiguration(configuration, metrics);
    

    Hope this helps!

提交回复
热议问题