如何设置字体的规模在自己的Android应用程序? [英] How to set font scale in my own android application?
问题描述
我想允许用户选择字体大小。
I want to allow user select font size.
备件确实提供该功能要做到这一点,
Spare Parts do provide the feature to do that,
ActivityManagerNative.getDefault()。updateConfiguration(新字体大小规模配置)
ActivityManagerNative.getDefault().updateConfiguration(new font size scale config)
但这是整个所有的Android应用程序。我想将其添加到我的应用程序的preference只为我的应用程序的影响。
but that is for whole all android applications. I want to add it in my application's preference and only impact for my app.
我试过下面的方法,它做作品的时候,但是我第一次开始我的应用程序,字体大小仍与1.0规模。我不知道为什么。我在下面加code在我活动的各个的onCreate。
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.
配置配置= resources.getConfiguration(); config.fontScale = 1.5F;
resources.updateConfiguration(配置,mDisplayMetrics);
Configuration config = resources.getConfiguration(); config.fontScale=1.5f; resources.updateConfiguration(config, mDisplayMetrics);
我该怎么办?
推荐答案
下code将允许更改应用程序只字体规模。
在第二行中,codeconfiguration.fontScale =(浮点)1;必须chabge满足您的需求。默认值是0.15步缩放,其默认为1。
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.
在调用的setContentView之前将其放置(R.layout.yourlayout)
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);
希望这有助于!
这篇关于如何设置字体的规模在自己的Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!