Set text size in .xml or programmatically

巧了我就是萌 提交于 2019-12-03 04:53:34

setTextSize( float ) expects a scaled pixel value. So, setTextSize( 12 ) would give you the desired result. However, getDimension() and getDimensionPixelSize() return the size in units of pixels, so you need to use the unit-typed variant of setTextSize() as follows:

setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );
tagButton.setTextSize(c.getResources().getDimensionPixelSize(R.dimen.tag_text_size));

this will work just fine :) You should also remember that textView has a setTextSize(int unit,float size), which should be used while setting size from code but not from xml dimen.

I have currently the same thing. Did set a dimension in dimens.xml and applied it programmatically, which is 3 times that big, than when settings via xml.

I checked also:

TextView.getTextSize() = 92f
getResources().getDimension(R.dimen ...) = 92f

TextView.setTextSize(92) != TextView with size from XML, other flags like TypedValue.COMPLEX_UNIT_PX make it even bigger.

The default setTextSize does apply COMPLEX_UNIT_SP by default btw. So once again, the Android API is inconsistent, so setting programmatically only and adapt sizes, so they fit, will be my solution.

Edit: Setting text size programmatically under Galaxy Note 2 (4.4.2) vs Note 4 (5.0.1) leads to a totally different result -.-

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