Android Adaptation Instructions 2

Source: Internet
Author: User

1 Basic settings
1.1 Androidmanifest.xml Settings

Add a child element in

Android:anydensity= "true" when the application is installed on a different density terminal, the program loads the resources in the xxhdpi, xhdpi, hdpi, MDPI, ldpi folders, respectively. Conversely, if set to False, the app does not automatically look for resources under the folder, even if it has the same resources under the folder:

1) If the drawable-hdpi, drawable-mdpi, drawable-ldpi three folders have a different density of the same picture resource, then the system will load the resources in the DRAWABLE_MDPI folder;

2) If there are high-density images in the drawable-hpdi, the other two folders do not have the corresponding picture resources, then the system will load the resources in the drawable-hdpi, other similar;

3) If there is a picture resource in the drawable-hdpi,drawable-mdpi, drawable-ldpi does not, the system will load the resources in drawable-mdpi, other similar, use the closest density level.
1.2 Horizontal Screen Vertical screen catalog distinction

1) drawable

A) drawable-hdpi the picture is applicable to the horizontal screen, but also for the vertical screen;

b) drawable-land-hdpi, when the screen is horizontal screen, and is high density, load the resources of this folder;

c) drawable-port-hdpi, load the resources in this folder when the screen is vertical and high density. The other same.

2) Layout

In the Res directory, the establishment of the Layout-port and Layout-land two directories, respectively, a vertical screen and two horizontal screen layout files, to adapt to the horizontal screen vertical screen automatically switch.
2 Multi-screen fit 4 gold principles

1) fill_parent, Wrap_content, Match_parent, and DP should be used to set the control dimensions in the layout file;

Specifically, when setting the values of the view's properties Android:layout_width and Android:layout_height, the Wrap_content,match_parent or DP is better than PX, and the text size should be defined using the SP.

2) do not appear in the code of the program specific pixel values, defined in the Dimens.xml;

To make the code simple, Android internally uses the PIX to represent the size of the control, but this is based on the current screen. To accommodate multiple screens, Android recommends that developers not use specific pixels to represent the size of the control.

3) do not use Absolutelayout (android1.5 is obsolete), you can use relativelayout substitution;

4) provide a suitable size picture for different screens.

Different size of the screen with different size of the picture, Low:medium:high:extra-high picture size ratio of 3:4:6:8; For example, for medium density (medium) screen your picture pixel size is 48x48, then low density The picture size of the screen should be 36x36, and the height (high) is 72x72,extra-high for 96x96.
3 using 9-patch PNG images

When using the picture resource, if there is a stretch, because the picture processing reason, will deform, cause the interface shape. The 9-patch PNG image is also a standard PGN image, vacated by a pixel interval around the native PNG image, to identify which parts of the PNG picture can be stretched, which cannot be stretched, the border position on the background, and so on.

"Top, left" defines an extruded area

"Right, bottom" defines the display area, and if you use a full-filled background map, it is recommended that you do not set the margin by android:padding, but by 9-patch way.

The Android SDK provides tools to edit 9-patch images, Draw9patch.bat in the tools directory to instantly see the edited stretch, or edit directly with other image editing tools, but no effect is visible.
4 different layout '
Android phone screen size varies, there are 480x320, 640x360, 800x480. How can application automatically adapt to different screens?
In fact, it is very simple to create a different layout folder in the Res directory, such as layout-640x360,layout-800x480, All layout files are written to R.java after compilation, and the system uses the appropriate layout according to the size of the screen.
5 Test verification

Typically use AVD Manager to create multiple emulators of different sizes

Android Adaptation Instructions 2

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.