Mobile phone and tablet multi-screen multi-resolution adaptation

Source: Internet
Author: User

From: http://www.apkbus.com/android-83006-1-1.html

# Participate in the android bus 2nd Anniversary Essay activity #


Recently, due to the wide development of Android, applications have gradually increased, more features have been learned, and the running platform has gradually expanded.

From the initial Mobile Phone platform to today's tablet computers, LCD TVs, and other electronic products, the resolution of cell phones is unknown.

In China, you never know how many cell phones and tablets are developed by shanzhai developers, not to mention dozens or even hundreds of LCD TVs.

The screen size of the mobile phone.

1. Basic Concepts
Screen size: the actual screen size, measured by the diagonal length of the screen (such as 3.4, 3.8 ). Android divides the screen into the following four types: small, normal, large, and extra large.

How to judge?

Screen density-the number of pixels in an actual screen area, measured by DPI (the number of dots per inch ). Compared with medium and high screen density devices, the L density of a fixed screen area has less pixels than that of A Low screen. Android classifies screen density into four types: low, medium, high, and extra high.

How can we determine whether it is ldpi, mdpi, or hdpi?

Orientation-screen orientation is divided into landscape and portrait ).

Resolution-the total number of workers on the screen. When you adapt a screen, you generally do not focus on its resolution, but on its screen size and density.

Density-independent pixels (density-independent pixel, DP or DIP)-to ensure that your UI is suitable for different screen density, we recommend that you use DP to define the program UI.

Its calculation method is: Px = DP * (DPI/160)


How can we identify ldpi, mdpi, and hdpi on a screen?

 

2. How to Adapt to multiple screens


A. Define the screen types supported by your program in manifest. The Code is as follows:

<Supports-screens Android: resizeable = ["true" | "false"]

Android: smallscreens = ["true" | "false"] // whether small screens are supported

Android: normalscreens = ["true" | "false"] // whether the screen is supported

Android: largescreens = ["true" | "false"] // whether the screen is supported

Android: xlargescreens = ["true" | "false"] // whether ultra-large screen is supported

Android: anydensity = ["true" | "false"] // whether screens of different density are supported

Android: requiressmallestwidthdp = "integer"

Android: compatiblewidthlimitdp = "integer"

Android: largestwidthlimitdp = "integer"/>

B. Provide different layout for screens of different sizes.

For example, to support a screen with a size of large, you need to create a folder named Layout-large/In the res directory and provide layout. Of course, you can also create the layout-port and layout-land directories under the res directory, which place two layout files, vertical screen and horizontal screen, respectively, to automatically switch the portrait screen.
 

C. Provide different images for screens of different density.

Try to use images in the format of. To provide suitable images for low-density screens, you need to create a folder named drawable-ldpi/and add images of the appropriate size. Correspondingly, medium corresponds to drawable-mdpi/, high corresponds to drawable-hdpi/, and extra high corresponds to drawable-xhdpi /.

Determine the image size: low: Medium: high: extra high ratio is 3: 4: 6: 8. For example, for a medium screen, if the pixel size of your image is 48 × 48, the low-density screen size should be 36 × 36, and the high) is 72 × 72, extra high is 96 × 96.

Drawable-hdpi this image is applicable to horizontal screens and vertical screen drawable-land-hdpi. When the screen is landscape screen and high density, resources in this folder are loaded.

Drawable-Port-hdpi: loads resources in this folder when the screen is portrait and high-density


D. dynamically change the density value in the code.
Sometimes, a value is dynamically set in the Code as needed. For example, the relative offset of the map address prompt box is different on mobile phones of different density. At this time, you can use the following method to find the screen density:
Displaymetrics metric = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (metric );
Int densitydpi = metric. densitydpi; // screen density DPI (120/160/240)
You can then set the offset for the density in the code.
However, it is best not to use this method. The best way is to set the phone numbers of different density in the XML file separately.
Here, the map offset can be set in the dimens. xml file in the values-HPDI, values-mdpi, and values-ldpi folders.


3. Four golden principles for multi-screen adaptation
A. wrap_content, fill_parent, and DP should be used to set the control size in the layout file.

Specifically, wrap_content, fill_parent or DP are better than pix when you set the value of the view attributes Android: layout_width and Android: layout_height. Correspondingly, SP should be used to define the text size in order to make the text size better adapt to the screen.

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

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.

C. Do not use absolutelayout. This is rarely used. relativelayout should be used.

D. provide images of the appropriate size for different screens.
4. How to test whether your program supports multi-screen adaptation

Generally, AVD manager is used to create multiple Simulators of different sizes, such

Reference: http://developer.android.com/guide/practices/screens_support.html

The article I promised last week was dragged because I was not in Shenzhen on weekends.-Sorry, I'm sorry, Prince.

It's not easy to catch up with the draft early in the morning. After reading the API, I used my experience in tablet development over the past two years. Finally, I would like to remind you that, the mainstream resolution is generally made up of five models. If someone says they want to adapt to all mobile phones, please remind him as soon as possible. It's not too early. Don't go to bed. Thank you.


Indicate the source of original article Transfer

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.