Android TextView setTextSize incorrectly increases text size [duplicate]

安稳与你 提交于 2019-11-28 07:08:46

Heh, mixed units problem. Seems a little counterintuitive, but it's an easy fix. The default method setTextSize(float) assumes you're inputting sp units (scaled pixels), while the getTextSize() method returns an exact pixel size.

You can fix this by using the alternate setTextSize(TypedValue, float), like so:

this.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

This will make sure you're working with the same units.

setTextSize() and getTextSize() work with different units. The parameter to set() is density-independent "scaled pixels", whereas get() returns plain old pixels.

pass units with size using TypedValue like below:

TypedValue.COMPLEX_UNIT_PX //Pixels

TypedValue.COMPLEX_UNIT_SP //Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels

setTextSize(TypedValue.COMPLEX_UNIT_SP, 18)

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