从设置阅读字体大小 [英] Read font size from Settings

查看:859
本文介绍了从设置阅读字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是一个几乎dublicate的 Android应用程序:如何阅读字体大小设置下我读的答案 CommonsWare 这点使用 Settings.System.FONT_SCALE 。所以我写这几行字:

This question is a almost dublicate of Android App: how to read Font Size under Settings? I read the answer of CommonsWare which points to use Settings.System.FONT_SCALE. So I wrote this few lines:

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的设备。顺便说一句我的code是在活动的onCreate 回调。

My problem is that I always get the SettingNotFoundException I'm using an Android 4.2.2 device. By the way I that code is in an onCreate callback of an Activity.

我也用Google搜索这个问题,并发现了大约3个站点,它使用相同的code。是否需要一些特殊的权限?我想也许可 android.permission.WRITE_SETTINGS 没有成功。

I also googled for that problem and found about 3 sites which uses the same code. Is some special permission required? I tried also the permission android.permission.WRITE_SETTINGS without success.

推荐答案

我刚刚发现的<一个源$ C ​​$ C href="http://developer.android.com/reference/android/provider/Settings.System.html"><$c$c>Settings.System这个功能:

I just found in the source code of Settings.System this function:

/** @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 的使用情况,以便我检查了<一href="http://developer.android.com/reference/android/content/res/Configuration.html"><$c$c>Configuration培训班里的文档指向 getResources()。getConfiguration()。所以我cound通过修复我的code:

There is however the FONT_SCALE in usage so I checked for that Configuration class where the documentation points to getResources().getConfiguration(). So I cound fix my code by using:

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

这篇关于从设置阅读字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆