Android's Margin and Padding attributes and supported length units

Android's Margin and Padding are the same as Html's. As shown in the figure below: the yellow part is Padding, and the gray part is Margin.

image

The popular understanding is that Padding is the inner border and Margin is the outer border

The corresponding properties are

android:layout_marginBottom="25dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:layout_marginRight="10dip"
android:paddingLeft="1dip"
android:paddingTop="1dip"
android:paddingRight="1dip"
android:paddingBottom="1dip"

If the left, right, top, and bottom are the same settings, you can set them directly

android:layout_margin="10dip"
android:padding="5dip"

 

The length units supported by Android.

  • px (pixels): points on the screen.
    pixels (pixels). Different devices have the same display effect. Generally, our HVGA represents 320x480 pixels, which is used more.
  • in (inch): unit of length.
  • mm (millimeter): unit of length.
  • pt (pound): 1/72 inch.
    point, is a standard unit of length, 1pt=1/72 inch, used in the printing industry, very simple and easy to use;
  • dp (density-independent pixel): An abstract unit based on screen density. On a 160 dots per inch display, 1dp = 1px.
  • dip: Same as dp, mostly used in android/ophone examples.
    device independent pixels (device independent pixels). Different devices have different display effects. This is related to the device hardware. Generally, we recommend using this to support WVGA, HVGA and QVGA, independent of pixels.
  • sp (scale-independent pixels): Like dp, but scaled according to the user's font size preference.
    scaled pixels (enlarged pixels). Mainly used for font display best for textsize.

In order to enable the user interface to be displayed normally on current and future display types, it is recommended that you always use sp as the unit of text size, and the default font size of Android also uses sp.

Use dip as the unit for other elements, such as length, height. Of course, you can also consider using vector graphics instead of bitmaps.

 

dp has nothing to do with density, and sp has nothing to do with scale besides density.

If the screen density is 160, then dp, sp, and px are the same. 1dp=1sp=1px, but if px is used as the unit, if the screen size remains unchanged (assuming it is still 3.2 inches), the screen density becomes 320.

Then the width of the original TextView is set to 160px, which is half shorter on a 3.2-inch screen with a density of 320 than on a 3.2-inch screen with a density of 160.

But if it is set to 160dp or 160sp. The system will automatically set the width attribute value to 320px.

That is 160*320/160. where 320/160 can be called the density scale factor. That is to say, if dp and sp are used, the system will automatically convert according to the change of screen density.

 

References:

Dip, dp, px, sp difference
http://sifutian.iteye.com/blog/680935

Guess you like

Origin blog.csdn.net/ghj1976/article/details/6365073