A summary of Android screen adaptation

Source: Internet
Author: User

Android uses two general properties to classify device screens: size and pixel density. As a developer, we have to design our applications to take into account that our applications are installed on devices with different screen sizes and pixel densities. Therefore, our app should include an optional resource to optimize the application appearance to support devices of different sizes and pixel densities.

There are four kinds of generalized dimensions: small, normal, large, xlarge.

There are also four generalized pixel densities: low (LDPI), Medium (MDPI), High (hdpi), Extra High (xhdpi)

In order to declare different layouts layouts and bitmaps resources for different screens, we have to place these optional resource files in separate directories . This is similar to the strings we do in different languages when we are internationalized .

Also note that the orientation of the screen (Landscape or portrait) is considered a change in screen size, so our application should modify the layout Layouts to improve the user experience in different screen orientations.

to improve the user experience on different device screens, we should create a unique XML for each screen size you want to support layout file. each layout file should be stored in the appropriate resource directory, which is suffixed with the screen size -<screen_size> . For example, a layout file for a large screen should be placed under the res/layout-large/directory.

It's important to note thatAndroid automatically scales your layout to fit the screen. Layout files for different screens, then, do not have to worry too much about The absolute size of the UI elements, but instead focus on the structure of the layout , which directly impacts the user experience (such as important view The size or position of the view should be relative to the sibling view view.

The layout filenames in the directory must be consistent, but their content can be different to provide an optimized UI to support the corresponding screen size

easy to get layout layout file references , Android system will be based on your app Span style= "FONT-FAMILY:CALIBRI;" >layout directory loads the layout file.  

We should also provide the appropriate bitmap resources that have been scaled for each of the generalized pixel densities : Low , medium Medium, high High and Extra High Extra-high pixel density. This will enable our application to achieve excellent graphics quality and rendering on all resolution devices.

In order to generate these picture resources, we should make the original picture resources in the format of the vectors, and then generate a picture of each resolution based on their scaled size:

    • Extra High Resolution XHDPI:2.0
    • High resolution hdpi:1.5
    • Medium resolution mdpi:1.0 (Benchmark)
    • Low resolution ldpi:0.75

This means that if we are going to generate a 200x200 image for a very high resolution device, then you need to generate 150x150 for the high-resolution device, the medium-resolution device generates 100X100, and the low-resolution device generates the 75X75 image.

These files are then placed in the appropriate resource directory. In the future when we refer to the pictures in the drawable , the system will automatically select the appropriate image resources based on the resolution of the device.

For resolution issues, the official solution is to create a different layout folder, which requires a layout file for each resolution of the mobile phone, although it seems to solve the resolution of the problem, but if one or more of the changes, it is necessary to make each layout file changes, which creates a lot of trouble. There are several ways to resolve this:

a) using Layout_weight

Currently the most recommended Android multi-screen adaptive solution.

The purpose of this property is to determine the display weight of the control in its parent layout, which is typically used in linear layouts. The smaller the value, the higher the priority of the corresponding layout_width or layout_height (generally to 100 is less obvious); In general, horizontal layout, the decision is the priority of the layout_width, vertical layout, the decision is Layout_ The priority of the height.

The traditional way to use layout_weight is to set the Layout_width and layout_height of the current control to fill_parent, so that the control's display scale is completely given to layout_weight; The smaller the layout_weight, the larger the display scale (i.e., the larger the weight, the smaller the effect of the display). However, for the 2 control is good, if the control too much, and the display scale is not the same time, the control is more troublesome, after all, the inverse is not so OK. So there is now the most popular 0px set value method. Seemingly incomprehensible layout_height=0px, combined with layout_weight, can make the control proportional to the display, easily solve the current Android development is one of the most headache fragmentation problem.

b) Manifest file configuration:(not recommended in this way, need to write different layouts for different interfaces)

You need to add a child element below the <manifest> element in the Androidmanifest.xml file

<supports-screensandroid:largescreens= "true"

Android:normalscreens= "true"

Android:anydensity= "true"

Android:smallscreens= "true"

Android:xlargescreens= "true" >

</supports-screens>

The above is a multi-resolution support for our screens (more accurately, the density of large, medium and small three).

Android:anydensity= "true", this sentence is very important to the entire screen, the value is true, our application when installed on a different density of mobile phones, the program will load the resources in the HDPI,MDPI,LDPI folder separately. Conversely, if the value is set to False, even if we have the same resource under the Hdpi,mdpi,ldpi,xdpi folder, the app does not automatically go to the appropriate folder for resources. Instead, the resources in the medium density mdpi file are loaded on large-density and small-density phones.

Sometimes it is necessary to set a value dynamically in code, you can set an offset for these densities in your code, but it is best not to use it, preferably in a different density phone in the XML file. The offset of the map here can be set in the Dimens.xml file in the values-xpdi,values-hpdi,values-mdpi,values-ldpi four folder.

When the screen is fit, you'll usually notice a few things:

First try to use linear layout, relative layout, if the screen does not fit, you can use ScrollView (can be dragged up and down)

Scrowview should be noted when using:

On different screens to show the content of different situations, in fact, this problem we often use a scrolling view to solve, that is, scrowview, it is important to note that the use of layout_weight in Scrowview is not valid, since the use of Scrowview, Set the size of the controls inside it to be fixed.

In addition, in the specified width, the unit with DIP, DP unit dynamic Matching

3, because the Android code is written in the units are pixels, all need to be converted through the tool class

4, try to use 9-patch diagram, you can automatically according to the picture above the content is stretched and shrunk. When editing, the gray area is stretched, the upper and lower two points control the horizontal direction of the stretching, left and right two points to control the vertical direction of stretching, 9-patch Diagram tool in the Adt-bundle-windows-x86\sdk\tools directory Draw9patch.bat

A summary of Android screen adaptation

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.