Android-screen Fit mode

Source: Internet
Author: User

fit: The current app shows the same effect on the same phone. Before adapting, you need to first determine the pixel density type of the current phone(such as:xhdpi,hdpi,mdpiwait)pixel density: The number of pixels distributed per inch, in units (DPI,PPI), the use of the Pythagorean theorem can be calculated.


according toGoogleofficial documents, pixel density is mainly divided into6A level.


Temporarily do not consider the situation of xxxhdpi, translated into Chinese, probably this means:
ldpi:120dpi,DPthe relationship to pixel conversions is:1DP = 0.75pxmdpi:160dpi,DPthe relationship to pixel conversions is:1DP = 1pxhdpi:240dpi,DPthe relationship to pixel conversions is:1DP = 1.5pxxhdpi:320dpi,DPthe relationship to pixel conversions is:1DP = 2pxxxhdpi:480dpi,DPthe relationship to pixel conversions is:1DP = 3px
Next, a brief introduction to the current marketFive common methods of adaptation

1 , picture adaptation

2 , Dimens Fitting

3 , layout file adaptation

4 , Java Code Adaptation

5 , weight adaptation


Adaptation method one: Picture adaptation

Mobile phone Loading project resource file (res) with different pixel densities pictures of different resources in

For example: Mi3 mobile phone, the main screen size is 5 inches , Home Screen resolution: 1920x1080 Pixel .

The pixel density is calculated according to the Pythagorean theorem:Sqrt(1920^2+ 1080^2)/5≈ 441dpi, or mi3 The screen pixel density is approximately equal to 441dpi.

according toGoogleThe partition of pixel density in official documents,Mi3The pixel density closest toxxhdpi, therefore, theMi3this phone to do picture adaptation, you need to followxxhdpiseparate cut chart.

Adaptation Method Two:Dimens.xmlfile Adaptation

    dimens.xml folder differs values ( :value-1280x720 value-800x480 values- xhdpi folder, can be used to specify the size of the control, different pixel density phone load different values dimens.xml adaptation, you need to write a number of files to adapt to the mainstream models on the market.

For example: the mobile phone assistant use Dimens Fitting


Adaptation Method III: Layout file adaptation

Different resolution of the phone, loading different layout files to achieve the adaptation effect.

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


suitable formula four: Java Code Adaptation

through Android corresponding API Gets the width-height pixel value of the current phone, proportionally assigns the width of the control in the screen to fit.

The core code (the width of the text box is set to half the width of the screen): TextView TV = (TextView) Findviewbyid (r.id.tv); Gets the encapsulated current phone screen information object for storing a wide height value displaymetrics metrics = new Displaymetrics (); Set the width height Getwindowmanager () to the current screen. Getdefaultdisplay (). Getmetrics (metrics); Get screen height int srceenheight = metrics.heightpixels; Gets the screen width int srceenwidth = metrics.widthpixels; The width is 50%relativelayout.layoutparamslayoutparams = new Relativelayout.layoutparams ((int) (srceenwidth*0.5+0.5), ( int) (srceenheight*0.5+0.5)); Tv.setlayoutparams (Layoutparams);

fit Way Five: Weight adaptation

through Android properties in Linear layout (layout_weight weights ) proportional to the allocation, has achieved the adaptation effect.

For example: Two buttons split the width of the screen

<button    android:layout_width= "0DP"     android:layout_weight= "1"     android:layout_height= "Wrap_ Content "/> < button    android:layout_width=" 0DP "     android:layout_weight=" 1 "     android:layout_ height= "Wrap_content"/>




Android-screen Fit mode

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.