About android's Unit (dp, sp) and androiddp
Android is a headache.
---------
Android has the following units:
Common dip and sp, sometimes using px.
---------
Two classes are introduced:
TypedValue: There are some methods related to unit conversion.
DisplayMetrics:
DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);
DisplayMetrics has fields related to these unit conversions:
---------
Their relationship:
Dip = DisplayMetrics. density * px
= (DensityDpi/160) * px
The sp and dip are the same, except that the sp varies with the device and is also affected by the user settings (the general path is: set-appearance-font size ). What makes people angry is that on Sony Xperia C3 (I don't know if all Xperia is like this), when the default font size is "normal, the actual display effect is the same as that of the "big" font on other machines. That is to say, the default font size settings in some systems are different from those in Android native systems. In this way, how do you control the display effect?
---------
Do you want to use dip, sp? Dip to use. To a large extent, the adaptation problem is solved. Sp to use? My suggestion is not to use it. It depends on you. If your app is dedicated to people with poor eyes (such as elders who work hard), you can directly specify a super large font. If you do not consider your eyesight, for the sake of beauty, do not let the user change the font size. If the two types are covered, use sp (not necessarily ~)!
Understanding of unit sp and dp in android UI
Dp is dip. This is basically similar to sp. You can use dp or sp to specify attributes such as length and height.
If you set the font, you need to use sp.
Dp is not related to density. sp is not only related to density, but also to scale. If the screen density is 160, dp and sp are the same as px. 1dp = 1sp = 1px, but if px is used as the unit, if the screen size remains unchanged (assuming it is still 3.2), the screen density is changed to 320. The original TextView width is set to 320 PX, And the 3.2-inch screen with a density of 160 is half shorter than the 3.2-inch screen with a density. But if it is set to 160dp or 160sp. The system automatically sets the width property value to 320px. That is, 160*320/160. Among them, 320/160 can be called the density proportion factor. That is to say, if dp and sp are used, the system will automatically convert according to the screen density change.
Next, let's take a look at the meaning of other units.
Px: the actual pixel of the screen. For example, 320*480 screens have 320 pixels horizontally and 480 pixels vertically.
In: indicates the inch, which is the physical size of the screen. Each inch equals 2.54 cm. For example, the screen size of a mobile phone is often described as 3.2 (English), 3.5 (English), and 4 (English. These dimensions are the diagonal length of the screen. If the phone screen is 3.2 inch, the diagonal line length of the phone screen (visible area) is 3.2*2.54 = 8.128 cm. You can measure your mobile phone screen to see if the size is the same as the actual size.
Mm: Indicates mm, which is the physical size of the screen.
Pt: indicates a point, which is the physical size of the screen. The size is 1 inch of 1/72.
How to Use dp sp in android
Dp (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a 160-point display per inch
Sp (pixels irrelevant to the scale): similar to dp, but can be scaled based on the user's font size preferences.
Knowing their unit knows how to use it.