Device settings independent font size

北慕城南 提交于 2019-12-02 00:52:10

问题


While developing my app I realised if someone changes device font size from normal, my application font sizes change too and it destroys some of the visuals I designed. So I wanted to ask if there is a way to fix text sizes independent from device's settings?

Thanks


回答1:


check the source code DisplaySettings.java, here is the method to control font size scale

public void writeFontSizePreference(Object objValue) {
    try {
        mCurConfig.fontScale = Float.parseFloat(objValue.toString());
        ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
    } catch (RemoteException e) {
        Log.w(TAG, "Unable to save font size");
    }
}

here is a solution keep the font size in different fontscale.

float textSize = 22f;
mTextView.setTextSize(textSize / getResources().getConfiguration().fontScale);

and I think @shoe rat's answer is more better for an activity or application context.




回答2:


If you want to completely ignore user's font size preferences, then use dp instead of sp for font sizes. The lint will complain about the dp usage (and rightly so, because you are potentially causing inconvenience to the user and possibly rendering your app unusable for visually impaired ones), but you should not face any runtime issues.

If you just want to ignore runtime font size changes, use "fontScale":

<application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    ... >
    <activity android:name=".MyActivity"
        android:configChanges="fontScale">
        ...
    </activity>
</application>



回答3:


Google recommends keeping your font sizes set with SP so that the UI settings will modify the font sizes within your application. Are you using SP?

I believe that using DP or PX or something else will cure your issue. Keep in mind that this is not recommended for Accessibility purposes.

For a complete list of possible text size types, see http://developer.android.com/reference/android/util/TypedValue.html

Here they are:

COMPLEX_UNIT_DIP - TYPE_DIMENSION complex unit: Value is Device Independent Pixels.
COMPLEX_UNIT_IN  - TYPE_DIMENSION complex unit: Value is in inches.
COMPLEX_UNIT_MM  - TYPE_DIMENSION complex unit: Value is in millimeters.
COMPLEX_UNIT_PT  - TYPE_DIMENSION complex unit: Value is in points.
COMPLEX_UNIT_PX  - TYPE_DIMENSION complex unit: Value is raw pixels.
COMPLEX_UNIT_SP  - TYPE_DIMENSION complex unit: Value is a scaled pixel.

Also: Note that not all of these options are available from an xml file and that some of these settings may only be used programmatically (at least that's what I recall).



来源:https://stackoverflow.com/questions/19817086/device-settings-independent-font-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!