Android size Issues

Source: Internet
Author: User

Android size problem (GO) (2013-01-15 16:55:36) reproduced
Tags: gossip Category: LINUX

Recent projects in the company have involved screen-adaptive problems. Because of the TV version of the project, the screen adaptive problem is more prominent. I remember writing an article like this before as a memo. Today deliberately on the computer turned a bit, and found it out, by the way also the reference to the article to find out. Here to share to everyone, hope to help you, have the wrong place also forget you correct.

Refer to the article address below, here to thank two authors.

http://blog.csdn.net/beihai1212/article/details/7026327

http://blog.csdn.net/moruite/article/details/6028547

First we look at screen resolution, pixel density, drawable and layout matching rules, and then give a screen-adaptive solution (also available on official documents).

1. Basic Concepts

in: That is, in inches, which represents the physical size of the screen. 1in = 2.54cm, and the size that we usually refer to is the length of the diagonal of the visible screen, not the screen area . So, for example, we say that a cell phone is 3.2 feet, that is, the diagonal length of its viewable area is: 3.2*2.54 = 8.128cm.

Resolution : It refers to the number of pixels in the vertical and horizontal direction of the screen. For example, the resolution is: 480*320, that is to say: Horizontal direction (width) has 320 pixels. The vertical direction (high) has 480 pixel points.

dpi: pixel density, or dots per inch, refers to the number of pixels per inch. such as 160dpi refers to the mobile phone horizontal or vertical direction of 160 pixels per inch distance. Assuming a device resolution of 320*240, the screen is 2 inches wide and 1.5 inches long, dpi=320/2=240/1.5=160. There are three main pixel densities in Android: 120,160,240. It is the value of the property densitydpi in the Displaymetrics class.

density: Density refers to the number of pixels per square inch. Calculation method: density= resolution/Screen size. in Android,the density of 160dpi is 1.0,120dpi density of 0.75,240dpi is 1.5. It is the value of the property density in the Displaymetrics class.

px: pixel, pixels. It shows the same effect on different devices. The "Same" here means that the pixels do not change. The first question: for example, if the width of a button is 100px, then the width of the device, whether it is 120,160 or 240 pixel density, should show 100px. , for example, we have three 480x320 mobile phones, and its dpi is: 120,160,180. So at this point, if it is displayed on the 160DPI mobile phone 3 cm, on the 120dpi phone will show (4/3) * * cm, more than the 160dpi display is bigger, the same reason, the button on the 240dpi appear more narrow. And here our cell phone is the same size, this is not suitable for screen problems.

This is one of the reasons we do not recommend not using PX because PX is directly related to the pixel density of the cell phone .

The second question: for example, we have three phones with a DPI of 120, and the screen size is "the value with the smaller width": 320*240,480*320,640*480. We need to put a button on the first line of the screen to make it fill the screen. Design, we set the size on the phone of 480*320, for example, 180px, when the program runs on 320*240 's mobile phone, and obviously, this button is not full, and in 640*480 's mobile phone It is also accounted for dissatisfaction. This is why we do not recommend the use of PX, because PX is directly related to the phone's screen .

Due to the above problem, we generally recommend using dip instead of PX in Android.

DP:DP is the abbreviation for dip, which is device-independent pixel, which is the device independent pixels. It is a unit that is independent of pixel density and screen size. There are different display effects on different screens. It is converted to pixel PX with the following formula: px = (pixel density/160) *dp.

As mentioned above, the pixel density commonly used in Android is 120,160,240. So on the 160dpi device, 1PX=1DP.

Solve the first problem: or three 480x320 phone, its dpi is: 120,160,180. We set the width of a button to: 100DP. Based on the conversion unit above, it displays a pixel of 100px on a 160dpi phone, which is 3cm above. Then on the 120dpi phone it shows the Pixel is (3/4) *100px, converted into centimeters: (4/3) * (3/4), or equivalent to the 160dpi display width, that is, 3cm. The same can be achieved on a 240dpi device with a display width of 3 cm.

SP: Enlarges the pixel, i.e. scaledpixels. It is mainly used for font display, and he will enlarge or abbreviate the font based on pixel density.

2. drawable and layout in Android.

for APIs 1.6 and later versions of the application, you can use DRAWABLE,DRAWABLE-HDPI,DRAWABLE-LDPI,DRAWABLE-MDPI, drawable-xdpi folders to use pictures at different pixel densities. Use Layout-small,layout-normal,layout-large,layout-xlarge to store layout files that are used in different screen sizes. The conversion relationships of the actual devices that they correspond to in the Android application are as follows:

The rules of the Android system to select the images stored in the drawable,drawable-hdpi,drawable-ldpi,drawable-mdpi,drawable-xdpi are: first to match the corresponding image resources under this density, if not, Go to use the picture resource under the drawable directory. For example, if a device has a pixel density of 240dpi, it will take precedence over the image resource under the drawable-hdpi directory, but if the drawable-hdpi directory does not exist, it will use the resource file in the drawable directory.

In addition to using the above five layouts in Android, you can also use LAYOUT-AXB and LAYOUT-SWADP to store layout files. Like Layout-480x360 and LAYOUT-SW600DP. After testing, the following matching rules are summed up:

Assume that the following layout file exists in an application:

layout-xlarge,layout-large,layout-normal,layout-small,layout,layout-sw500dp,layout-1280x800,layout-1280x888, LAYOUT-1280X750,LAYOUT-SW1280DP,LAYOUT-SW1DP.

For all versions of the API (the 2.2,3.0,3.2,4.0 test is the following result):

A, first to match Layout-xlarge, followed by Layout-large, then Layout-normal, and finally Layout-small. Any one of the above can match successfully, and the layout file will match successfully.

B, if the match is not successful, will go to find layout-swadp this file, if you can find ADP can be smaller than the screen width, matching success. Even if your screen is: 1280x800, if there is layout-sw1dp in layout, and the first condition is not matched, and LAYOUT-SWADP is the only one, then at this time, no matter what other layout files, LAYOUT-SW1DP will go to match, Even if you have layout-1280x800 in your layout file.

C, if the above two conditions are not matched to, this time to be divided version to match the LAYOUT-AXB file:

3.2 Previous Versions:

Use this layout file only if layout-axb exactly matches, otherwise use layout. In other words, my screen is 1280x800, The API version is 1.6-3.0, then if there is layout-1280x750 in the layout file and there is no layou-1280x800, it is said that can not exactly match, then the system will choose Layout file to match.

3.2 Later versions:

If LAYOUT-AXB can match exactly, use this layout file, if not match, go to find A and B are smaller than its screen, and the closest to the layout file. But the premise is: if it is a large screen, you give the layout file size than normal, if it is a small screen, you give the layout file is smaller than normal.

3, Screen Adaptive solution

A, use Wrap-content and Match-parent (fill-parent before api2.2), that is, width and height are consistent with content adjustment and stretching to the parent control. The size of the dead control instead of hard-coded write.

B. Using relative layout (relativelayout)

It is flexible, but also relatively complex, to use relative layouts for the placement of controls.

C, using the framelayout, that is, the frame layout can be to some extent to eliminate the problem caused by screen size.

D, use Layout-xlarge,layout-large,layout-small this way to build multiple layout files. In this way, you can match all apps that are older than 1.6 with just four layout files. And the effect is much better than just using one layout.

E, using the minimum width identifier, that is, the Layout-swaaaadp method. In the new features of 3.2, the authorities specifically stated the following four ways to use the 3.2 version of the layout recommended by the tablet, as follows:

Res/layout-sw600dp/main_activity.xml//7-inch tablet

Res/layout-sw720dp/main_activity.xml//10-inch tablet
Res/layout-w600dp/main_activity.xml//Adaptive to width
Res/layout-sw600dp-w720dp/main_activity.xml//ultra-wide layout

F, use LAYOUT-AXB this way to do screen adaptive.

This layout file is used only when the LAYOUT-AXB is fully matched before the 3.2 release, but it can be the closest match after 3.2, all of which can be laid out in the TV edition.

G, some important pictures, such as logo, the main page of those images can use 9.png images, because the image will not be very serious distortion after stretching, so the display effect will be better than ordinary PNG pictures.

More information on 9.png images can be seen here: http://www.himigame.com/android-game/321.html

H, use drawable-hdpi,drawable-mdpi this way to define different pictures, can help us to adapt to different screen density. Using DP and SP can help us to better layout

Android size problem (GO)

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.