Font size of TextView in Android application changes on changing font size from native settings

前端 未结 14 2078
抹茶落季
抹茶落季 2020-12-04 07:47

I want to specify my own text size in my application, but I am having a problem doing this.

When I change the font size in the device settings, the font size of my

相关标签:
14条回答
  • 2020-12-04 08:24

    Actually, Settings font size affects only sizes in sp. So all You need to do - define textSize in dp instead of sp, then settings won't change text size in Your app.

    Here's a link to the documentation: Dimensions

    However please note that the expected behavior is that the fonts in all apps respect the user's preferences. There are many reasons a user might want to adjust the font sizes and some of them might even be medical - visually impaired users. Using dp instead of sp for text might lead to unwillingly discriminating against some of your app's users.

    i.e:

    android:textSize="32dp"
    
    0 讨论(0)
  • 2020-12-04 08:24

    You can use this code:

    android:textSize="32dp"
    

    it solves your problem but you must know that you should respect user decisions. by this, changing text size from device settings will not change this value. so that's why you need to use sp instead of dp. So my suggestion is to review your app with different system font sizes(small,normal,big,...)

    0 讨论(0)
  • 2020-12-04 08:26

    change the DP to sp it is good pratice for android android:textSize="18sp"

    0 讨论(0)
  • 2020-12-04 08:29

    If units used are SP, not DP, and you want to override system font scaling, all you do is override one method in activity - see this post.

    resources.updateConfiguration is deprecated and buggy (on Oreo I had issues with formatted text).

    0 讨论(0)
  • 2020-12-04 08:29

    in android 8 this solution work for me

    add this code in base activity

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        final Configuration override = new Configuration(newBase.getResources().getConfiguration());
        override.fontScale = 1.0f;
        applyOverrideConfiguration(override);
    }
    

    source https://stackoverflow.com/a/57225687/7985871

    0 讨论(0)
  • 2020-12-04 08:30

    Also note that if the textSize is set in code, calling textView.setTextSize(X) interprets the number (X) as SP. Use setTextSize(TypedValue.COMPLEX_UNIT_DIP, X) to set values in dp.

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