Android screen Adaptation

Source: Internet
Author: User

Android There are five main ways of screen adaptation: Picture adaptation, layout adaptation, size adaptation, weight adaptation, code adaptation. The first two are not commonly used, the latter three kinds of use more.

Picture adaptation

  • Open 4 type of resolution simulator
  • in the drawable multiple directories with different content but naming the same picture
  • Run program View the display effect on different emulators
  • General Practice Art only do a set of 1280*720 Span style= "font-family: Equal line; Background-color:white "> picture placed in drawable-xhdpi under the directory height specified as determined value Not wrap screen

Layout adaptation

  • for specific resolutions , Create Layout folder : layout-800x480, Layout-land ( horizontal screen )
  • 800x480 comparison with other resolution simulators
  • General Practice : this way is not a last resort , generally not

Size adaptation

DP and PX relationships: dp=px/device density
Acquisition of device density (density):

' Float density = getresources (). Getdisplaymetrics(). density;

??

According to this, we can write a DP Mutual Transfer px of the Util class with the following code:

public static Int dp2px (Context context,float DP)
{
float Density=context.getresources (). Getdisplaymetrics (). density;
return (int" (Dp*density+0.5f);
}

PublicStatic float PX2DP (context context,int px) {
Float density=context.getresources (). Getdisplaymetrics (). density;
return px/density;
}

??

  • General equipment Density : 0.75, 480x320 (1), 800x480 (1.5), 1280x720 (2)
  • Set DP value , view the displayed scale on a different screen
  • Create a folder values-1280x720, in the Dimens.xml size in the set , Adaptation Screen
  • General practice : artist provides pixels px We need to use px divided by device density convert to DP after written in layout file

of course, usingDPdoes not solve all problems, such as800x480(vertical screen) and1280x720(horizontal screen), it is not possible to set only oneDPvalue, we can write the general value in theDimens.xml, create avalues-1280x720folder will1280x720of the screenDPvalues are written in this folder.Dimens.xmlinside. This way, when you encounter1280x720the device will be loadedvalues-1280x720in the folderDimens.xmlthe value.

Weight Matching

we know that only inLinearLayoutlayout, onlyLayout_weightproperty. This property is used to controlLinearLayouteach sub-component in the layout is displayed at a certain scale. Generally speaking,Layout_weightThe larger the value, the greater the proportion. The calculation formula is: the width (or height) of a component isLinearLayoutThe ratio is: this componentWeightvalue/LinearLayoutAll components withinWeightthe value of the and.

Code adaptation

int width = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();
int height = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();

Tv1.setlayoutparams (New Layoutparams ((int) (width*0. 5), (int) (height*0. 2)));

General Practice: If you are a custom control that does not use an XML layout file, you can dynamically set the width height in your code

Android screen Adaptation

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.