2013-05-14 137 views
11

这个问题是几乎dublicate Android App: how to read Font Size under Settings?我看了CommonsWare的答案,它指向使用Settings.System.FONT_SCALE。所以我写这几行字:从设置中阅读字体大小

float scale = -66.6f; 
try { 
    scale = android.provider.Settings.System.getFloat(getContentResolver(), 
            android.provider.Settings.System.FONT_SCALE); 
} catch(SettingNotFoundException e) { 
    Log.e(getClass().getSimpleName(), "Error: ", e); 
} 
Toast.makeText(this, "scale=" + scale, Toast.LENGTH_LONG).show(); 

我的问题是,我总是得到SettingNotFoundException我使用的是Android 4.2.2设备。顺便说一下,我的代码在ActivityonCreate回调中。

我也搜索了这个问题,发现了大约3个使用相同代码的网站。是否需要特殊许可?我也尝试了许可android.permission.WRITE_SETTINGS没有成功。

+0

嗯,我不确定你需要阅读这个。只要你使用SP单元,系统不会为你扩展吗? – Teovald 2013-05-14 14:09:05

+0

当然,但我有一些图形错误,我需要修复这个比例因子。 – rekire 2013-05-14 14:22:27

回答

27

我只是在Settings.System源代码中发现了这个功能:

/** @hide */ 
public static void getConfigurationForUser(ContentResolver cr, 
             Configuration outConfig, int userHandle) { 
    outConfig.fontScale = Settings.System.getFloatForUser(
     cr, FONT_SCALE, outConfig.fontScale, userHandle); 
    if (outConfig.fontScale < 0) { 
     outConfig.fontScale = 1; 
    } 
} 

然而有在使用的FONT_SCALE所以我检查为Configuration类,其中文件指向getResources().getConfiguration()。所以我通过使用下列代码修复我的代码:

float scale = getResources().getConfiguration().fontScale; 
3

您可以根据字体比例设置系统字体。

例如:对于巨大的字体比例为2.0,那么你可以设置如下。

Configuration config = new Configuration(); 
config.fontScale = 2.0f; 
getResources().getConfiguration().setTo(config); 
0

添加 “浮动DEF” 参数到 公共静态浮动getFloat的端部(ContentResolver的CR,字符串名称,浮法DEF)

实施例:

规模= android.provider.Settings .System.getFloat(getActivity()。getContentResolver(), android.provider.Settings.System.FONT_SCALE,1f);