Part of the source code:
/*** Set The default text size to a given unit and value. See {@link* Typedvalue} for the possible dimension units. * * @paramunit the desired dimension unit. * @paramsize the desired size in the given units. * * @attr ref Android. R.styleable#textview_textsize*/ Public voidSettextsize (intUnitfloatsize) {Context C=GetContext (); Resources R; if(c = =NULL) R=Resources.getsystem (); ElseR=c.getresources (); Setrawtextsize (typedvalue.applydimension (unit, size, R.getdisplaymetrics ())); } Private voidSetrawtextsize (floatsize) { if(Size! =mtextpaint.gettextsize ()) {mtextpaint.settextsize (size); if(Mlayout! =NULL) {nulllayouts (); Requestlayout (); Invalidate (); } } } /*** Converts an unpacked complex data value holding a dimension to it final floating * point value. The Parameters <var>unit</var> and <var>value</var> * is as in {@link#TYPE_DIMENSION}. * * @paramUnit The unit to convert from. * @paramvalue The value to apply the unit to. * @paramMetrics current display metrics to use in the conversion--* Supplies display density and Scalin G information. * * @returnThe complex floating point value multiplied is the appropriate * metrics depending on its unit. */ Public Static floatApplydimension (intUnitfloatvalue, Displaymetrics metrics) { Switch(unit) { Casecomplex_unit_px:returnvalue; CaseComplex_unit_dip:returnValue *metrics.density; Casecomplex_unit_sp:returnValue *metrics.scaleddensity; Casecomplex_unit_pt:returnValue * metrics.xdpi * (1.0f/72); Casecomplex_unit_in:returnValue *metrics.xdpi; Casecomplex_unit_mm:returnValue * metrics.xdpi * (1.0f/25.4f); } return0; }
1. By default:
<name= "text_size">20sp</dimen>
float size =
At this point, size gets a value of 20, but the font size set by TextView is: Setrawtextsize (metrics.scaleddensity);
2. Set the font size to 20:
float size =