Android adaptive screen size and Layout

Source: Internet
Author: User

I. Different Layout
The screen sizes of Android mobile phones vary from 480X320,640x360,800x480,854 X. How can an app 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. However, when adding a layout file based on the resolution, the original folder and resource of layout must exist. Otherwise, an error may occur. At the same time, when naming the layout folder, you must follow this rule. The large number of layout-640x360 placed before the decimal, otherwise it will report an error! For example:

Even if this is set, you will find that the mobile phone does not select Layout Based on the resolution. Why? After multiple tests, you can find the problem here:

It turns out that the screen size was chosen as an exception. You only need to set all the XML layout files in the layout folder to non-m options. You can set the default size you need.

Here, I select 2.7 as the default.

This wayProgramSelect the layout File Based on pixels after startup.

Address: http://blog.csdn.net/chenlei1889/article/details/6291282

 

 

 

 

About Android nodpi, xhdpi, hdpi, mdpi, and ldpi

Author: lizongbo published at: 22: 43. Wednesday, January 12 th, 2011
Copyright statement: You can reprint it at will. Please mark it as a hyperlink during reprinting.ArticleSource and author information and this copyright notice.
Http://618119.com/archives/2011/01/12/205.html

About Android nodpi, xhdpi, hdpi, mdpi, and ldpi

First, there are several basic concepts:
1. Screen Size
That is, the actual size of the display screen is measured based on the diagonal line of the screen.
For the sake of simplicity, Android divides all the screen sizes into four types: small, normal, large, and super large (corresponding to small, normal, large, and extra large ).
Applications can provide different custom screen la s for these four dimensions-the platform will select the corresponding layout for rendering based on the actual screen size, which is transparent to the program side.

2. screen aspect ratio Aspect Ratio
The aspect ratio is the ratio of the physical width to the physical height of the screen. Applications can use limited resources to provide screen layout resources for the specified aspect ratio.
3. Screen Resolution resolution
The total number of physical pixels displayed on the screen. Note that although the resolution is usually expressed in width x height, the resolution does not mean the specific aspect ratio of the screen.
In the andorid system, the application does not directly use the resolution.

4. Density
Specify the number of pixels that can be displayed within the physical width and height range based on the pixel resolution.
In the same wide and high area, the low-density display displays less pixels, while the high-density display displays more pixels.
Screen density is very important, because when other conditions remain unchanged, a total of wide and high Fixed UI components (such as a button) appear very large on the low-density display, on a high-density display screen, it looks small.
For simplicity, Android divides all screen resolutions into four sizes: small, common, large, and ultra-large (corresponding to small, normal, large, and extra large ).
Applications can provide different resources for these four dimensions-the platform transparently scales the resources to adapt to the specified screen resolution.

5. Device Independent pixel density-independent pixel (DP)
Applications can be used to define virtual pixel units of the UI component and describe the layout size and position in a density-independent manner.
A device independent pixel is equivalent to a physical pixel on a 160 DPI screen.
When the program is running, the system transparently processes independent pixel units of any devices that need to be scaled according to the actual density of the screen. The conversion of independent pixels of devices into actual pixels of the screen is very simple: pixels = DPS * (density/160 ).
For example, on a 240 DPI screen, an independent device pixel equals 1.5 physical pixels. to ensure that the UI components can be properly displayed on different screens, we strongly recommend that you use device independent pixel units to define your application UI.

Four types of screen sizes: small, normal, large, and xlarge
Four density classifications: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)
It should be noted that xhdpi is added from Android 2.2 (API Level 8.
Xlarge is a category added from Android 2.3 (API level 9.
DPI is the abbreviation of "dot Per Inch". The number of dots per inch.

Normal screen: ldpi is 120, mdpi is 160, hdpi is 240, and xhdpi is 320.

Reference: http://developer.android.com/images/screens_support/screens-ranges.png

Two methods to obtain screen resolution information:

Displaymetrics metrics = new displaymetrics ();
Display display = activity. getwindowmanager (). getdefadisplay display ();
Display. getmetrics (metrics );
// The pixel value obtained here is the device independent pixel DP

// Displaymetrics metrics = activity. getresources (). getdisplaymetrics (); The obtained parameter information is incorrect. Do not use this method.
Android. content. res. Resources. getsystem (). getdisplaymetrics () cannot be used (). The obtained width and height are empty.
If you need to customize resource files for Android pad, the directory under the res directory may be:
Drawable
Drawable-ldpi
Drawable-mdpi
Drawable-hdpi
Drawable-xhdpi
Drawable-nodpi
Drawable-nodpi-1024 × 600
Drawable-nodpi-1280 × 800
Drawable-nodpi-800 × 480
Values
Values-ldpi
Values-mdpi
Values-hdpi
Values-xhdpi
Values-nodpi
Values-nodpi-1024 × 600
Values-nodpi-1280 × 800
Values-nodpi-800 × 480

Refer:

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

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.