1. Different Layout
The screen sizes of Android mobile phones vary by 480X320,640x360,800 X. How can an app automatically adapt to different screens? In fact, it is very simple, just need to create different layout folder under the res directory, such as: layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size.
2. hdpi, mdpi, and ldpi
In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.
Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:
Drawable-hdpi stores high-resolution images, such as WVGA (480x800) and fwvga (480X854)
Drawable-mdpi stores medium-resolution images, such as hvga (320x480)
Drawable-ldpi stores low-resolution images, such as qvga (240x320)
The system will find the corresponding images in these folders according to the machine resolution. To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.
Screen direction
1. Automatic horizontal and vertical screen Switching
You can create two layout-port and layout-land directories under the res directory, which are placed in two layout files: vertical screen and horizontal screen, in this way, the system automatically calls the corresponding layout file when the screen direction of the mobile phone changes, to avoid the problem that one layout file cannot meet the two screen display conditions.
2. Disable Automatic Switch
You only need to add the Android: screenorientation attribute restriction to the androidmanifest. xml file.
Android: screenorientation = "Landscape" // restrict horizontal display on this page
Android: screenorientation = "portrait" // restrict the number of pages displayed on this page.
Adaptive font size
Method 1:
First, different font sizes are obtained based on different resolutions.
Create in res
In values-480x320/strings. XML, set <dimen name = "text_size"> 30px </dimen>
And
In values-800x400/strings. XML, set <dimen name = "text_size"> 30px </dimen>
The font size of 480x320 and X is 30px and 40px respectively;
This is called in a Java file.
Int sizeoftext = (INT) This. getresources (). getdimension (R. dimen. text_size );
Method 2:
Obtain the view width in onsizechanged of the view. Generally, the default width is 320. Therefore, calculate a zoom rate = (float) W/320 W to indicate the actual width.
And then paint when setting the font size. settextsize (INT) (8 * rate); 8 is the actual font size to be set when the resolution width is 320 = default font size x rate