Android Screen adaptation research

Source: Internet
Author: User

In the project development process, usually have to encounter screen adaptation problems, the following is my project in the development process summed up
A set of screen adaptation program, share to everyone;

The Android screen fits some concepts that you must understand:

Px
is the abbreviation for the English word pixel, which means pixels, dots on the screen. The resolution we usually refer to as 480x800 is the pixel.

Dpi
DPI is the abbreviation for dots per inch, in dots, which contains the number of pixels per inch. such as the 320x480 resolution of the phone, 2 inches wide, 3 inches high, the number of pixels per inch is 320/2=160dpi (landscape) or 480/3=160dpi (portrait), 160 is the dpi of the phone, landscape and portrait of this value is the same, The reason is that most phone screens use a square pixel point.

Screen density, density and dpi are density = dpi/160

Dp
Also known as dip, device independent pixel, devices independent pixels abbreviation, Android Special Unit, on screen density dpi = 160 screen, 1DP = 1px.


Sp
and DP very similar, generally used to set the font size, and DP is the difference is that it can be based on the user's font size preference to zoom.

Android drawable Directory Description:

DRAWABLE-LDPI (dpi=120, density=0.75)

DRAWABLE-MDPI (dpi=160, Density=1)

DRAWABLE-HDPI (dpi=240, density=1.5)

DRAWABLE-XHDPI (dpi=320, density=2)

DRAWABLE-XXHDPI (dpi=480, density=3)

The above is the Android drawable directory of the meaning of each folder, the system will be based on the resolution of the machine to each of these folders to find the corresponding picture.

Let's talk about the solution to the screen adaptation problem I've encountered:

1, how to adapt to a variety of screens

A. Define the screen types supported by your program in manifest and the corresponding code is as follows:
<supports-screens android:resizeable=["true" | "False"]
Android:smallscreens=["true" | "false"]//whether small screen support
Android:normalscreens=["true" | "false"]//whether to support the middle screen
Android:largescreens=["true" | "false"]//whether to support large screen
Android:xlargescreens=["true" | "false"]//whether to support the super-large screen
Android:anydensity=["true" | "false"]//support multiple screens with different densities
android:requiressmallestwidthdp= "Integer"
android:compatiblewidthlimitdp= "Integer"
android:largestwidthlimitdp= "integer"/>

B. Different layout for different sizes of screens

Android phone screen size is different, there are 480x320, 640x360, 800x480. How can I get the app to adapt to various screens automatically?
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.

C. horizontal screen vertical screen automatic switch


The Res directory can be established in the layout-port-800x600 and Layout-land two directories, which are placed in vertical screen and horizontal screen two layout files, so that the mobile phone screen direction changes when the system will automatically call the corresponding layout file, Avoid a layout file that does not meet the two screen display issues.
Different resolution horizontal screen vertical screen automatically switch:
Using 800x600 as an example
layout-port-800x600 and layout-land-800x600 Two directories can be established in the Res directory


D. Provide different images for different densities of screens.

In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements.
Should try to use a point 9 format of the picture, if you want to the low density of the screen to provide the appropriate picture, you need to create a new folder drawable-ldpi/, and put in the appropriate size of the picture. Accordingly, the medium corresponds to drawable-mdpi/,high corresponding to Drawable-hdpi/,extra high corresponding to drawable-xhdpi/.
The size of the picture is determined: the Low:medium:high:extra high ratio is 3:4:6:8. For example, for medium density (medium) screens where your picture pixel size is 48x48, the low-density screen should have a picture size of 36x36, and a high of 72x72,extra for 96x96.

2, multi-screen adaptation of the 4 gold principles
A. You should use Wrap_content,fill_parent and DP when setting the control dimensions in the layout file.

Specifically, the wrap_content,fill_parent or DP is better than the PIX when setting the values of the view's properties Android:layout_width and Android:layout_height. Correspondingly, the screen should use the SP to define the size of the text in order for the text to fit better.

B. Do not show specific pixel values in the code of the program.

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.

C. Do not use Absolutelayout (android1.5 is obsolete). Accordingly, the relativelayout should be used.

D. Provide a picture of the right size for different screens. See part 1th above.

Android Screen adaptation research

Related Article

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.