1. Commonly Used units: the relative units are px, sp, and dp.
Absolute unit: pt, in, mm
Ii. Unit Application Summary: Generally, relative units are used instead of absolute units.
1. the font size is generally SP. The font in this unit can be scaled automatically based on the font size you set.
2. relative distances such as space generally use dp (dip). As the density changes, the number of corresponding pixels also changes, but there is no direct relative ratio change.
3. px is related to actual pixels and density! There is no relationship between dp and sp and actual pixels. For screens with a certain resolution but different density, the application of px units may lead to a change in the relative length ratio.
Iii. Density and resolution:
The density value indicates the number of display points per inch, and the resolution is two concepts.
The screen density standard is: HVGA screen density = 160; QVGA screen density = 120; WVGA screen density = 240; WQVGA screen density = 120
Specific Application relationship: assume that the resolution is x * y, the density is d, and the actual screen size is a * B. Then the relationship is x * y = d * a * B (approximately equal)
For screen resolution information of different density, the actual screen resolution is 240 PX * 400px when density = 120 (two points correspond to one resolution) in the case of Dip * 800dip WVGA (density =)
Iv. comparison summary:
1. If the resolution is different for the same density (I .e. the same entity screen), the corresponding units sp and dp irrelevant to the entity density are displayed normally.
2. In the case of the same resolution and different density, because the standard density is usually used, the analysis is of little significance.
Other materials:
Px: The pixel of the screen.
In: inches
Mm: mm
Pt: LB, 1/72 inch
Dp: an abstract unit based on density. If a screen of DPI is displayed, 1dp = 1px.
Dip: equivalent to dp
Sp: similar to dp, but it also scales according to the user's font size preference.
We recommend that you use sp as the unit of text, and dip for others.
The following is an overview of the relationship between dip and px:
HVGA screen density = 160; QVGA screen density = 120; WVGA screen density = 240; WQVGA screen density = 120
The value of density indicates the number of display points per inch, and resolution.
For details about the screen resolution of different density, take the WVGA (density = 240) of 480dip * 800dip as an example.
When density = 120, the actual screen resolution is 240px * 400px (two points correspond to one resolution)
The height of the status bar is 19px or 25dip.
The screen width is 400px or 800dip, and the working area height is 211px or 480dip.
Screen width: Px or dip, working area Height: 381px or 775dip
When density = 160, the actual screen resolution is 320px * 533px (three points correspond to two resolutions)
The height of the status bar is 25px or 25dip.
The screen width is 533px or 800dip, and the working area height is 295px or 480dip.
Screen width: 320px or 480dip, working area Height: 508px or 775dip
When density = 240, the actual screen resolution is 480px * 800px (one point for one resolution)
The height of the status bar and title bar is 38px or 25dip.
The screen width is PX or dip, and the working area height is PX or dip.
Screen width 480px or 480dip, working area height 762px or 775dip
In the apk resource package, when the screen density is 240, the resources using the hdpi label
Resources that use the mdpi label when the screen density is 160
Ldpi tag resources are used when the screen density is 120.
Resources without any tags are shared in various resolutions.
Use the unit dip as much as possible during layout, and use less px
The following describes the mutual conversion of several different units.
Copy codeThe Code is as follows:
Public static int dip2px (Context context, float dipValue ){
Final float scale = context. getResources (). getDisplayMetrics (). density;
Return (int) (dipValue * scale + 0.5f );
}
Public static int px2dip (Context context, float pxValue ){
Final float scale = context. getResource (). getDisplayMetrics (). density;
Return (int) (pxValue/scale + 0.5f );
}
Public static int dip2px (Context context, float dipValue ){
Final float scale = context. getResources (). getDisplayMetrics (). density;
Return (int) (dipValue * scale + 0.5f );
}
Public static int px2dip (Context context, float pxValue ){
Final float scale = context. getResource (). getDisplayMetrics (). density;
Return (int) (pxValue/scale + 0.5f );
}
The following describes how to obtain the Resolution:
Write the following code in the onCreate method of an Activity:
Copy codeThe Code is as follows:
DisplayMetrics metric = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (metric );
Int width = metric. widthPixels; // screen width (pixels)
Int height = metric. heightPixels; // screen height (pixels)
Float density = metric. density; // screen density (0.75/1.0/1.5)
Int densityDpi = metric. densityDpi; // screen density DPI (120/160/240)
This is quite simple, but have you tried it on a 800*480 machine? Is it 533 in width? Because the default density of android is 1.0 at the beginning, you can add it to manifest. xml.
1. uses-sdk node, <uses-sdk android: minSdkVersion = "4"/>, indicates that your apk cannot be installed on machines earlier than sdk1.6.
2. supports-screens node.
Copy codeThe Code is as follows:
<Supports-screens
Android: smallScreens = "true"
Android: normalScreens = "true"
Android: largeScreens = "true"
Android: resizeable = "true"
Android: anyDensity = "true"/>