Inconsistency when setting TextView font size in code and in resources

假如想象 提交于 2019-11-29 20:23:27
maxmc

You should use setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); because the documentation of the getDimension method states that it returns a Resource dimension value multiplied by the appropriate metric. which I understand to be the precalculated absolute px value.

That is, use:

tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size_small));
DaRolla

Somehow this seems to fit:

XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="typo14">9sp</dimen>
</resources>

Java:

setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.typo14));

Its a matter of sp px dpi

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