Android Learning-screen fit

Source: Internet
Author: User
Tags naming convention

For Android application development friends should know that the current Android phone screen type is very diverse, size, length and width ratio are not the same, the same interface in different resolutions of the mobile phone may appear layout confusion, control movement phenomenon. The adaptation of the screen is not possible to achieve exactly the same interface effect shown on each screen. Can only rely on the usual programming habits and experience is the interface in different screen size will not appear excessive in the scope of the meeting to control the interface under different screen changes, and make corresponding adjustments. So how do we fit the screen? Let me say it in my development experience.

1, try to use linear layout (linearlayout) and relative layout (relativelayout), do not use absolute layout.

2, try to use dip and SP, do not use PX.

3. Provide different layout files and pictures for different resolutions.

4, set the multi-resolution support in Androidmainfest.xml.

5. If you need to fit at a specific resolution, you canResNew in DirectoryLayout-hxw.xmlthe folder. For example, to fit1080*1800the screen (MeizuMX3with this resolution) the newLayout-1800x1080.xmlfolder, and then define the layout below. AndroidThe system will first look for layouts with the same resolution, and if not, use the defaultLayoutbelow the layout.

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.

In the field of design, pixels are the smallest unit used to calculate digital imagery. The images displayed in the computer are not composed of contiguous lines, but are made up of a number of invisible dots. If you enlarge the image several times, you will find that these continuous tones are actually composed of a number of small dots of similar color, which are the smallest unit "pixels" that make up the image.

    • Inch

Represents inches, which is the physical size of the screen. Equal to 2.54 centimeters per inch. For example, we often say that the size of the mobile phone screen, 5 (English), 4 (English) inches refers to this unit. These dimensions are the diagonal length of the screen. If the phone's screen is 4 inches, the diagonal length of the phone's screen (viewable area) is 4 X 2.54 = 10.16 centimeters.

    • 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.

    • Density

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.


Layout_weight Property

This property is actually very useful, and is generally used in conjunction with android:layout_height= "Fill_parent" or android:layout_width= "Fill_parent", which is used to describe the proportion of the child element's size in the remaining space. Add a row only a text box, then its default value is 0, if there are two equal-length text boxes in a row, then their android:layout_weight value can be the same as 1. If there are two unequal text boxes in a row, their android:layout_weight values are 1 and 2, respectively, then the first text box will occupy two-thirds of the remaining space, and the second text box will occupy One-third of the remaining space. Android:layout_weight follows the principle that the smaller the value, the higher the importance. Android screen so many, can not be scaled, so the simple use of dip to set the control is not feasible, with the Layout_weight attribute flexible use, enhance the adaptability of the interface.

drawable Resources

First: You need to add a child element as follows in the Androidmanifest.xml file

<supports-screens android:anydensity= "true" android:largescreens= "true" android:normalscreens= "true" Android: Smallscreens= "true" android:xlargescreens= "true"/>

As the name implies, the above is a multi-resolution support for our screen (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 folder, then the application will not automatically go to the appropriate folder to find resources, this is in high-density, and low-density mobile phones, such as a 240x320 pixel cell phone, If you set android:anydensity= "false", the Android system will convert 320 (low density) to 320x480 (medium density), so the app will load the resources in the mdpi file on the small-density phone.
Careful people will find that since android2.0 started drawable files are replaced by three folders drawable-hdpi,drawable-mdpi,drawable-ldpi three folders, some programmers in order to let the application load some pictures by default , they will deliberately re-create the Drawable folder in the application after android2.0, in fact it is not necessary to do so, through the first paragraph of the analysis we learned that the android:anydensity= "false", then the application will be the size of density into medium density To load the resources in the MDPI. Here again, when android:anydensity= "false", the app will load the resources in the MDPI.

To summarize:

First: Android:anydensity= "true", the system will automatically find the corresponding folder according to the screen density

Second: android:anydensity= "false",

(1) If the drawable-hdpi,drawable-mdpi,drawable-ldpi three folders have different densities of the same picture resource, then the system will load

Resources in the DRAWABLE_MDPI folder

(2) If there is a high density image in the drawable-hpdi, the other two folders do not have the corresponding picture resources, then the system will load the resources in the drawable-hdpi.

(3) If there is a picture resource in the drawable-hdpi,drawable-mdpi, there is no corresponding picture resource in the drawable-ldpi, then the system will load the resources in the Drawable-mdpi folder.

Note the different representations of the various folders.

DRAWABLE-HDPI the picture is applied to the horizontal screen, also for the vertical screen

DRAWABLE-LAND-HDPI, when the screen is horizontal and high density, load the resources in this folder

DRAWABLE-PORT-HDPI, when the screen is vertical and high density, the resources in this folder are loaded

Draw9patch Tools

An image processing tool is provided in the tools directory of the Android SDK package. Sometimes we need image resources adaptive and not distorted, whether horizontal screen or vertical screen, high resolution or low resolution, can be filled automatically, and not distortion and so on background problems. Android in this case, a. 9.PNG format was specifically created to address this issue. Draw9patch is a gadget that helps we make a. 9.PNG format resource.
*.9.png in the standard PNG format, just the outermost circle of the extra 1px of the border, this 1px border is used to define the image of the extensible and static invariant area. In particular, the cross-section of the left and top (top) borders is an extruded part, and the unselected part is the static area section. The cross-section of right and bottom (bottom and right) borders is the content part.

650) this.width=650; "class=" Fit-image "width=" 244 "height=" 199 "border=" 0 "title=" clip_image009 "alt=" clip_image009 "Src=" http://s3.51cto.com/wyfs01/M01/0A/1E/wKioJlGcLAazFxmqAAAeiYBQi6g732.jpg "style=" border:0px none; Padding-left:0px;padding-right:0px;padding-top:0px;background-image:none; "/>

In other words, in the phone. No matter how the above picture is enlarged, the rounded corners are not magnified! Only the left and top (top) borders will stretch!
Images with 9.PNG format in Android do not have backgrounds, so you can customize the stretch without distortion, such as the system button is a typical example. In fact, whether it is left and top, or right and bottom are to divide the picture into 9 pieces (corner Four is not scalable, the other four is allowed to scale), so called 9. Png.
About the use of draw9patch tools in this do not elaborate, you need to go to Baidu for a bit.

Layout resources

Sometimes, because the screen layout is too complex, the same layout file can be difficult to adapt to all screen resolutions. You can create multiple corresponding layout folders under the Res folder of the project, like Drawable Resources, and write the appropriate layout file for the screen of the corresponding resolution.

The naming convention for folders is this: layout-resolution
For example: layout-320x240, layout-480x320 and so on.
The layout file under the appropriate folder will be loaded in the system based on the resolution of the operating environment.


Android Learning-screen fit

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.