Android Development screen adaptation scheme

Source: Internet
Author: User

Android Screen Adaptation Program summary
 日常开发中,难免遇到屏幕适配的方案,网上也有很多方案,在这里本人只是做一个总结,以免日后使用
Concept of the Unit
    • PT (Point): lbs, a standard fixed-length unit with a size of 1/72 inches, mainly for printing and graphic design. 1 inch = 2.54 cm = 72 lb 1pt=0.03527cm

    • PX (pixel): pixels, the smallest point that makes up the screen image, a relative size unit. The smaller the pixels in the same-size screen, the higher the screen resolution, and the higher the contrast.

      1pt= (PPI/72) *px

    • Screen Size: The physical size of the screen, which refers to the length of the diagonal of the screen, not the length or height of the screen. Units in inches, 4 inch, 5 inch mobile phone ...
      Android divides all screen sizes into four sizes: small, normal, large, oversized (corresponding to small, normal, large, extra large)

    • Resolution (Resolution): The total number of pixels the screen has and the number of pixels in the horizontal (wide) and vertical (high) directions of the screen.
      ?

    • PPI (pixels per inch): screen density, which refers to the number of pixels per inch/pixel. Higher PPI values represent the display's ability to display images at higher densities. The higher the density shown, the higher the fidelity.

    • DPI (dots per inch): Print counting unit, which is the number of dots per inch that can be printed. One of the main parameters that measure the printer's printing accuracy, the higher the value, the higher the printer's print accuracy. PPI=DPI on the electronic screen

    • DP (density-independent Pixels): screen-independent pixel units. Android Development Virtual unit, at 160 dots per inch on the monitor, 1dp=1px;

    • SP (scale-independentpixels): Android font size virtual unit. On a monitor 160 dots per inch, 1sp=1px when the text size is 100%. If the font is set to SP, the font can vary depending on the size of the system.

Adaptation Program
    • folder directory different resource adaptation
      1, set up a folder corresponding to the screen resolution, such as a screen resolution of 540*960, you can establish the corresponding resource folder values-960x540, This will read the corresponding file information according to the resolution.

      2, consider the horizontal screen and vertical screen layout effect: Android has Layout-port and layout-land two directories placed vertical screen and horizontal screen of two layout files, the mobile phone screen changes, will automatically call the corresponding layout file.

      3, suitable for large screen, sometimes need to consider a tablet-like screen for adaptation, you can put the XML layout file in the Layout-large folder, the name of this folder and layout under the same arrangement, Screens that exceed 7 inches will automatically read the files under large. (Note: Before android3.2 version trial)

      4, the version after android3.2 can use the LAYOUT-SW600DP folder, SW is the abbreviation of small width, see the meaning of the minimum width is known. The rules of this folder are the same as the Layout-large, which represents the width and height of the minimum side of the 600DP layout, which is more than 600DP screen.

      5, layout aliases: Sometimes in the SW600DP file and large folder or multiple folders need to maintain layout files, this time we also write two copies of the code is a bit redundant, we need to set the layout alias to maintain a common set. In layout can only 2 files one is a small screen default such as: Main.xml, the other is a large screen such as: Main_large.xml. Then you configure the alias under values,
      Default: Values/layout.xml
      Minimum width 600dp:values/layout-sw600dp/layout.xml code is as follows
      Greater than 7 inches before 3.2: The Values/layout-large/layout.xml code is as follows

<?xml version= "1.0" encoding= "Utf-8"?     <resources ;  <item  name  = "main"  type  = "layout" ;  @layout/main_large</item  >  </resources ;   
    • adaptation when writing XML
      1, try to use Wrap_content, match_parent, and weight. This will automatically expand the layout. For example, weight weights, use the time can be allocated according to proportion, there will be no display of the same phenomenon.

      2, the use of relative layout, because the Relativelayout is based on the location of the control, so we can choose the corresponding reference, such as a button set on the right side of the parent layout, so that, regardless of the parent layout, no matter how wide or how high, this button is on the right side of the parent layout.

    • the adaptation of pictures and resources
      1, we can adapt to different values, you can set a number of screens are dimens files, because on the 720 screen 1DP equals 2px to 1080 on the screen equals 3px, so we can set different PX or DP values according to different dimens in the Dimens file.

      2, cut different sizes of the drawings to fit. Can feel umeng to see the most mainstream in the current market, with the most mobile phone screen information, cut the corresponding set of pictures is the most commonly used adaptation method. However, the picture does not suggest too much, because this will affect the size of the APK package. We can compress these pictures, by the way a compressed tool site, the pro-test effect is good.

      By the way recommend a compressed image of the tool site, the effect is good oh!

      3, 9patch image use, we call it point 9, this tool is under the SDK, online tutorial A lot of I do not describe.

    • Fit in the code

      1、我们在自定义控件中,可以根据代码获取到当前的屏幕密度,然后对空间的大小进行设置,这样的话设置的代码就是动态计算宽度和高度的。如我们用画笔花的字体,会在不同分辨率上呈现不同的大小,这时候可以通过获取分辨率然后计算出我们要设置的宽度和高度。2、其实在写代码时候经常会遇到在代码中计算的情况,例如我弹出一个单选框,需要动态设置高度,如果写死一个高度的话会对不同的选项有问题。


This can be dynamically adapted by setting the Layoutparams to dynamically set the width of a space and other properties of the higher.

    • Other adaptations that come to mind will continue to be replenished.

Android Development screen adaptation scheme

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.