Customize controls to set the text size

Source: Internet
Author: User

Generally, when we place text in our own controls, we will use the paint:

Paint paint = new paint (); paint. setantialias (true); paint. settextsize (16 );

However, if you are on a mobile phone with other resolutions and view the text effects, there will be problems,

If debugging is performed on a low-resolution machine on a high-resolution machine, the font size is too small. Otherwise, the font size is too large.

Therefore, you cannot set the text font in the above way, but should use a method similar to textview, such:

Public void settextsize (INT textsize) {context c = getcontext (); resources R; If (C = NULL) r = resources. getsystem (); else r = C. getresources (); int size = (INT) typedvalue. applydimension (typedvalue. complex_unit_sp, textsize, R. getdisplaymetrics (); mtextpaint. settextsize (size );}

This also encourages us to use SP as the measurement unit rather than PX when setting the textview font 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: 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.