Device settings independent font size

前端 未结 3 1442
南旧
南旧 2021-01-21 04:11

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 wan

相关标签:
3条回答
  • 2021-01-21 04:47

    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>
    
    0 讨论(0)
  • 2021-01-21 05:04

    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).

    0 讨论(0)
  • 2021-01-21 05:12

    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.

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