Solution of Android font size adaptation to different resolutions

Solution of Android font size adaptation to different resolutions

Today, someone asked me how to adapt the font size of mobile phones with different resolutions and sizes of Android system? In fact, the adaptation of font and picture is the same.

1、 The principle is as follows:

Suppose you need to adapt to the resolution of 320x240480x320. Create new folders values-320x240 and values-480x320 in the res directory. Then in the folder values,

Create a new XML file dimensions.xml under values-320x240 and values-480x320. The contents of the XML file are as follows:

The contents of dimensions.xml under vaules-320x240 are as follows:

The contents of dimensions.xml under values-480x320 are as follows:

For mobile phones with different resolutions, the Android system will automatically adapt and load the font size value in the corresponding file. The value of btntextsize is different. The reference method in the layout file is as follows:

Call in the java file as follows:

Through this method, it is convenient to set the font size at different resolutions. Of course, not only the font size, width and height, but also some other attributes can be set in a similar way!

2、 1. Get the width of the view in the onsizechanged of the view. Generally, the default width is 320, so calculate a zoom ratio

2. 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

Layout multi-resolution adaptation:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>