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