How does the font size of Android adapt to different resolutions?

Source: Internet
Author: User

How does the font size of Android adapt to different resolutions?

Someone asked me today how to adapt the font size of mobile phones with different resolutions and sizes in the android system? In fact, the adaptation of fonts and images is a truth.

I,

The principle is as follows:

Assume that the resolution is 320x240,480 x320. Create a folder values-320x240, values-480x320 under the res directory. Then create the xml file dimens. xml under the folder values, values-320x240, and values-480x320. The content of the xml file is as follows:

The content of dimens. xml in the vaules-320x240 is as follows:

 
 
  
   18sp
  
 
The content of dimens. xml in the values-480x320 is as follows:

 
 
  
   18sp
  
 
For mobile phones with different resolutions, the android system automatically adapts to the font size values in the corresponding file. The btnTextSize values are different. The reference method in the layout file is as follows:

 
The following code is called in a java file:

Int size = (int) this. getResources (). getDimension (R. dimen. Text_size );

This method makes it easy to set the font size at different resolutions. Of course, you can set the font size, width, and other attributes in a similar way!


II,

1. Obtain the view width in onsizechanged of the view. The default width is 320. Therefore, a scaling ratio is calculated.
Rate = (float) w/320 w is the actual width
2. Then, when setting the font size, paint. setTextSize (int) (8 * rate); 8 is the font size to be set when the resolution width is 320
Actual font size = default font size x rate

 
Multi-Resolution layout adaptation:


Technical Consultation: please pay attention to the subscription number, name: not a famous programmer, Number: smart_android

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.