how to prevent system font-size changing effects to android application?

后端 未结 8 2204
执笔经年
执笔经年 2020-11-27 03:49

I recently finished developing my android application. I used sp (Scaled pixels) for all textSize. The problem is when i adjusted the system font-size, my application\'s fon

8条回答
  •  春和景丽
    2020-11-27 04:23

    I think use dp is the best way, but in some case you may want to use a font style, but the style is using sp, you can convert sp to dp by:

    fun TextView.getSizeInSp() = textSize / context.resources.displayMetrics.scaleDensity
    
    fun TextView.convertToDpSize() = setTextSize(TypedValue.COMPLEX_UNIT_DIP, getSizeInSp())
    

    so you can use the sp value from style without dynamic font size, and no need to hardcode the font size

提交回复
热议问题