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

前端 未结 14 2079
抹茶落季
抹茶落季 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:31

    It is not a good thing to have to specify DIP or SP again by code when already defined in a dimen.xml file.

    I think that the best option is to use PX when using a dimen.xml value :

    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_size));
    

    This way, you can switch from DP to SP if needed in dimen.xml file without having to change any code.

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

    this may help. add the code in your custom Application or BaseActivity

    /**
     * 重写 getResource 方法,防止系统字体影响
     *
     * @return
     */
    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1) {
            Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }
    

    however, Resource#updateConfiguration is deplicated in API level 25, which means it will be unsupported some day in the future.

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

    this solutions is with Kotlin and without using the deprecated function resources.updateConfiguration

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            adjustFontScale(resources.configuration)
        }
    
        private fun adjustFontScale(configuration: Configuration?) {
            configuration?.let {
                it.fontScale = 1.0F
                val metrics: DisplayMetrics = resources.displayMetrics
                val wm: WindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
                wm.defaultDisplay.getMetrics(metrics)
                metrics.scaledDensity = configuration.fontScale * metrics.density
    
                baseContext.applicationContext.createConfigurationContext(it)
                baseContext.resources.displayMetrics.setTo(metrics)
    
            }
        }
    

    Observation: this is the same solution as the above but udpated with Kotlin

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

    Use the dimension type of resources like you use string resources (DOCS).

    In your dimens.xml file, declare your dimension variables:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <dimen name="textview_height">25dp</dimen>
      <dimen name="textview_width">150dp</dimen>
      <dimen name="ball_radius">30dp</dimen>
      <dimen name="font_size">16sp</dimen>
    </resources>
    

    Then you can use these values like this:

    <TextView
       android:layout_height="@dimen/textview_height"
       android:layout_width="@dimen/textview_width"
       android:textSize="@dimen/font_size"/>
    

    You can declare different dimens.xml files for different types of screens. Doing this will guarantee the desired look of your app on different devices.

    When you don't specify android:textSize the system uses the default values.

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

    Override getResources() in Activity.

    Set TextView's size in sp as usual like android:textSize="32sp"

    override fun getResources(): Resources {
        return super.getResources().apply {
            configuration.fontScale = 1F
            updateConfiguration(configuration, displayMetrics)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 08:41

    for kotlin, it works for me

    priceTextView.textSize = 12f
    
    0 讨论(0)
提交回复
热议问题