Note when setting the font size dynamically.
When we dynamically set the font size for android, we stepped on some pitfalls and listed them to remind everyone.
1. setTextSize () directly. The default unit is sp.
2,
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopViewStyle);
textSize = typedArray.getDimension(R.styleable.TopViewStyle_textSize, TypedValue.applyDimension(TypedValue .COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.title_size), context.getResources().getDisplayMetrics()));
Custom Attributes are used to obtain the font size of a custom view, as shown in the code above. However, the Unit obtained in this way is px, so setTextSize () cannot be directly given. Otherwise, the displayed size will change greatly.
3. How can we solve this problem?
setTextSize(TypedValue.COMPLEX_UNIT_PX,18); //pxsetTextSize(TypedValue.COMPLEX_UNIT_SP,18); //spsetTextSize(TypedValue.COMPLEX_UNIT_DIP,18);//dip
Use the preceding method to specify the unit. In fact, set setTextSize () directly. The parameter is the default TypedValue. COMPLEX_UNIT_SP: The final font size of the above three lines of code is the same, but the unit is different, all of which are converted by 18sp.
Respect the fruits of labor, Mount please indicate the source http://www.cnblogs.com/tangZH/p/8298499.html