How to set font scale in my own android applicatio

2019-02-10 15:42发布

I want to allow user select font size.

Spare Parts do provide the feature to do that,

ActivityManagerNative.getDefault().updateConfiguration(new font size scale config)

but that is for whole all android applications. I want to add it in my application's preference and only impact for my app.

I tried below methods, it do works sometimes, but the first time I start my application, the font size still with the 1.0 scale. I do not why. I added below code in every onCreate of my activities.

Configuration config = resources.getConfiguration(); config.fontScale=1.5f; resources.updateConfiguration(config, mDisplayMetrics);

What can I do?

3条回答
一纸荒年 Trace。
2楼-- · 2019-02-10 15:56

I think what you are looking for is to apply a theme to your activity or view

Please take a look at the android docs Themes

查看更多
我想做一个坏孩纸
3楼-- · 2019-02-10 16:01

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!

查看更多
唯我独甜
4楼-- · 2019-02-10 16:05

Translated for Xamarin....

        Android.Content.Res.Configuration configuration = Resources.Configuration;
        configuration.FontScale = (float)1; //0.85 small size, 1 normal size, 1,15 big etc

        Android.Util.DisplayMetrics metrics = new DisplayMetrics();
        this.WindowManager.DefaultDisplay.GetMetrics(metrics);
        metrics.ScaledDensity = configuration.FontScale * metrics.Density;
查看更多
登录 后发表回答