Android-Screen Adaptation

Source: Internet
Author: User

Android-Screen Adaptation
Adaptation: the current application displays the same effect on the same mobile phone. Before adaptation, you must first determine the pixel density type (such as xhdpi, hdpi, and mdpi) to which the current mobile phone belongs. pixel density: number of pixels distributed per inch, unit (dpi, ppi ), it can be calculated by using the stock theorem.
Vc7EtbWjrM/xy9jD3LbI1vfSqrfWzqo2uPa1yLy2oaM8YnI + CjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20150304/20150304093721102.png" alt = "\">

If xxxhdpi is not considered for the moment, it is translated into Chinese:
Ldpi: 120 dpi, the relationship between dp and pixel is: 1dp = 0.75 pxmdpi: 160 dpi, the relationship between dp and pixel is: 1dp = 1 pxhdpi: 240 dpi, the relationship between dp and pixel conversion is: 1dp = 1.5 pxxhdpi: 320 dpi, the relationship between dp and pixel conversion is: 1dp = 2 pxxxhdpi: 480 dpi, and the relationship between dp and pixel conversion is: 1dp = 3px
Next, we will briefly introduce the five adaptation methods that are commonly used on the market.

1. Image adaptation

2. dimens adaptation

3. layout file adaptation

4. java code adaptation

5. Weight adaptation


Adaptation Method 1: Image adaptation

Mobile phones with different pixel density load different resource images in engineering resource files (res)

For example, for the mi3 mobile phone, the size of the main screen is 5 inch, and the resolution of the main screen is X pixels.

The pixel density of the mi3 is calculated based on the hook theorem: Sqrt (1920 ^ 2 + 1080 ^ 2)/5 ≈ 441 dpi, that is, the pixel density of the mi3 screen is approximately 441 dpi.

According to the division of pixel density in the official google Documents, the pixel density of mi3 is the closest to xxhdpi. Therefore, if you want to adapt the image of mi3, You need to split the image separately according to xxhdpi.

Method 2: dimens. xml file adaptation

Dimens. xml exists in different values (such as value-1280x720, value-800x480, values-xhdpi) folders in the project resources (res) folder and can be used to specify the control size, mobile phones with different pixel density load dimens under different values folders. xml files, which are usually adapted to dimens, need to write multiple files to adapt to mainstream models on the market.

Example: 360 mobile assistant uses dimens adaptation


Adaptation Mode 3: Layout file adaptation

For mobile phones with different resolutions, load different layout files to achieve the adaptation effect.

Create multiple layout (for example: layout-1280x720, layout-800x480) folders to store layout files required for phones of different pixel density.


Method 4: java code adaptation

Use the corresponding android api to obtain the width and height pixels of the current mobile phone, and allocate the width and height of the control in the screen proportionally to achieve the adaptation effect.

Core code (set the width and height of the text box to half the width and height of the screen): TextView TV = (TextView) findViewById (R. id. TV); // get the information object encapsulated on the current mobile phone screen, used to store the wide and high value DisplayMetrics metrics = new DisplayMetrics (); // set the wide and high getWindowManager () for the current screen (). getdefadisplay display (). getMetrics (metrics); // get the screen height int srceenHeight = metrics. heightPixels; // get the screen width int srceenWidth = metrics. widthPixels; // the width and height respectively account for 50% RelativeLayout. layoutParamslayoutParams = new RelativeLayout. layoutParams (int) (srceenWidth * 0.5 + 0.5), (int) (srceenHeight * 0.5 + 0.5); TV. setLayoutParams (layoutParams );

Adaptation Method 5: weight adaptation

The linear layout attribute (layout_weight weight) in android is allocated in proportion to achieve adaptation.

For example, the two buttons divide the screen width equally.

 < Button    android:layout_width="0dp"     android:layout_weight="1"     android:layout_height="wrap_content"/>




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.