`

程序中使用dimension中定义的尺寸大小不对的原因

阅读更多
程序中使用dimension中定义的尺寸大小不对的原因 (2013-02-26 09:18:26)转载▼
When you define a dimension in a resource file you include the measurement unit. If you use sp units they are scaled according to the screen density so text at 15sp should appear roughly the same size on screens of differing density. (The real screen density of the device isn't going to exactly match as Android generalises screen density into 120, 160, 240 and 320 dpi groups.)

When calling getResources().getDimension(R.dimen.textsize) it will return the size in pixels. If using sp it will scaled by the screen density,

Calling setText(float) sets the size in sp units. This is where the issue is ie you have pixels measurements on one hand and sp unit on the other to fix do this:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
Note you can also use

getResources().getDimensionPixelSize(R.dimen.textSize);
instead of getDimension() and it will round and convert to an non fractional value.
dimen 必须设置 px
<dimen name="listview_largetextsize">18px</dimen>
<dimen name="listview_smalltextsize">14px</dimen>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics