Android adaptation 2. android adaptation instructions
1. Basic settings
1.1 AndroidManifest. xml settings
ADD child element in
Android: anyDensity = "true": When an application is installed on a terminal of different density, the program loads Resources in the xxhdpi, xhdpi, hdpi, mdpi, and ldpi folders respectively. On the contrary, if it is set to false, even if the folder has the same resources, the application will not automatically search for resources in the corresponding folder:
1) if the drawable-hdpi, drawable-mdpi, and drawable-ldpi folders have different density representations of the same image resource, the system will load Resources in the drawable_mdpi folder;
2) If drawable-hpdi contains high-density images and there are no corresponding image resources in the other two folders, the system will load resources in drawable-hdpi;
3) If drawable-hdpi, drawable-mdpi contains image resources, and drawable-ldpi does not, the system loads resources in drawable-mdpi. Similarly, the closest density level is used.
1.2 distinguish between landscape and portrait screens
1) drawable
A) drawable-hdpi: this image is suitable for horizontal screen and vertical screen;
B) drawable-land-hdpi. Resources in this folder are loaded when the screen is a horizontal screen with a high density;
C) drawable-port-hdpi: loads resources in this folder when the screen is a portrait screen with a high density. Similarly.
2) layout
Create the layout-port and layout-land directories under the res directory, and place the layout files of the portrait and landscape screens respectively to automatically switch the landscape and landscape screens.
2 multi-screen adaptation 4 gold principles
1) fill_parent, wrap_content, match_parent, and dp should be used to set the control size in the layout file;
Specifically, when you set the value of the view attributes android: layout_width and android: layout_height, wrap_content, match_parent or dp are better than px. The text size should be defined by sp.
2) do not show specific pixel values in the program code, which are defined in dimens. xml;
To make the code simple, android uses the pix to represent the widget size in units, but this is based on the current screen. To adapt to multiple screens, android recommends that developers do not use specific pixels to represent the widget size.
3) do not use AbsoluteLayout (android1.5 has been deprecated). You can use RelativeLayout instead;
4) provide images of the appropriate size for different screens.
Images of different sizes are used for screens of different sizes. The ratio of low: medium: high: extra-high to 3: 4: 6: 8. For example, for medium) the pixel size of your image is 48 × 48, so the low-density (low) screen size should be 36 × 36, and the high (high) is 72 × 72, extra-high is 96 × 96.
3. Use a 9-patch PNG Image
When image resources are used, if the image is stretched, the image will be deformed due to image processing, resulting in page shape. 9-patch PNG is also a standard PGN image, with a pixel interval left around the native PNG image, it is used to identify which parts of a PNG image can be stretched, which cannot be stretched, and the border position on the background.
"Up and left" defines the stretched Area
"Right and bottom" define the display area. If you use a fully filled background image, we recommend that you do not use android: padding to set the margins, but use the 9-patch method to define the display area.
The Android SDK provides the 9-Patch image editing tool. In the tools directory, draw9patch. bat, you can immediately see the stretching effect after editing, you can also directly use other image editing tools to edit, but do not see the effect.
4 different layout'
The screen sizes of Android mobile phones vary by 480x320,640x360,800 x. How can I enable the Application to 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.
5 test and verification
Generally, AVD Manager is used to create multiple Simulators of different sizes.