Setting the TextView font size on Java code

Source: Internet
Author: User

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 =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.