Read font size from Settings

前端 未结 4 1975
情深已故
情深已故 2020-11-30 10:52

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_SCA

相关标签:
4条回答
  • 2020-11-30 11:34

    You can set system font according to font scale.

    Example: For huge font scale is 2.0 then you can set as follow.

    Configuration config = new Configuration();
    config.fontScale = 2.0f;
    getResources().getConfiguration().setTo(config);
    
    0 讨论(0)
  • 2020-11-30 11:35

    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;
        }
    }
    

    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;
    
    0 讨论(0)
  • 2020-11-30 11:43

    Add "float def" param to the end of the

    public static float getFloat(ContentResolver cr, String name, float def)
    

    Example:

    scale = android.provider.Settings.System.getFloat(getActivity().getContentResolver(),
                                        android.provider.Settings.System.FONT_SCALE, 1f);
    
    0 讨论(0)
  • 2020-11-30 11:44

    You could probably use getResources().getDisplayMetrics().scaledDensity - http://developer.android.com/reference/android/util/DisplayMetrics.html#scaledDensity

    0 讨论(0)
提交回复
热议问题