Android dimension size

Source: Internet
Author: User

First, let's take a look at several concepts.

Screen size

The actual physical size is calculated based on the diagonal line of the screen. For example, iPhone 4 is a 3.5 inch screen, that is, its screen diagonal line is long. 1 inch is about 25.4mm. The size unit points mentioned later is equal to 1 inch of 1/72.

For simplicity, Android divides all the actual screen sizes into four broad dimensions: small, normal, large, and extra large.

Screen density screen Density

The number of pixels in the physical area of the screen, usually referred to by DPI (dots per inch points per inch. For example, a low-resolution screen must have fewer records in the same physical area than a medium-resolution or high-resolution screen.

For simplicity, Android divides all the actual screen density into four general density: low, medium, high, and extra high.

Is Android's Division of screen size and screen density:


Resolution resolution
Total number of physical pixels on the screen. For example, Samsung rugby III has a resolution of 240x320 pixels. 240x320 pixels means that there are 240 pixels on each horizontal line of the mobile phone screen, 320 pixels in each vertical column, and 320x240 = 76800 pixels.

Dimension size
The dimension value defined in XML. A dimension value is a value with a dimension unit, such as 10px, 2in, and 5sp. The following are supported dimensions in Android:
DP
Density-independent pixels density-an abstract unit of screen-based physical density. This size unit is based on the screen of 160 DPI (dots per inch), that is, on such a screen, 1dp is approximately 1px. On a higher-density screen, the number of pixels used to indicate 1 DP increases proportionally (depending on the appropriate factor determined by the screen DPI); similarly, on a low-density screen, the number of workers used to indicate 1dp is reduced proportionally. It indicates that the number of records in 1dp changes according to the screen density, but not necessarily proportional. Using DP instead of PX as the size unit is a simple solution that enables the view in the layout file on devices of different density to be adjusted appropriately. In other words, it keeps the size of UI elements on different devices consistent in the real world.
SP
Scale-independent pixels ratio independent pixels-similar to DP, but also influenced by the user's font size settings. It is recommended to set the font size so that the actual font display effect is adjusted based on the screen density and the font size.
PT
Points point-the screen-based physical size (physical size), equal to 1 inch of the 1/72.
Px
Pixels pixel-the actual pixel of the screen. The number of pixels per inch varies by device. Therefore, the total number of pixels varies by device. Therefore, we do not recommend using this dimension unit because the actual performance of different devices varies.
Mm
Millimeters millimeter-screen-based physical size (physical size ).
In
Inches inches-physical screen-based size (physical size ).

In short, SP is used to set the font size, and DP is used for others.

For more information, see http://blog.csdn.net/welovesunflower/article/details/7930248.


Note:

A dimension resource is a simple resource, that is, it is applied based on the value of the specified name attribute (rather than the name of the XML file containing it ). Therefore, you can place dimension resources and other simple resources under the <resources> element of the same XML file.

File Location:
res/values/filename.xml
The filename is arbitrary. <dimen>Element's nameWill
Be used as the resource ID.
Resource reference:
In Java: R.dimen.dimension_name
In XML: @[package:]dimen/dimension_name
Syntax:
<?xml version="1.0" encoding="utf-8"?><resources>    <dimen        name="dimension_name"        >dimension</dimen></resources>
Elements:
<resources>
Required.This must be the root node.

No attributes.

<dimen>
A dimension, represented by a float, followed by a unit of measurement (DP, SP, PT, PX, mm, in), as described above.

Attributes:

name
String. A name for the dimension. This will be used as the resource ID.
Example:
XML file saved res/values/dimens.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <dimen name="textview_height">25dp</dimen>    <dimen name="textview_width">150dp</dimen>    <dimen name="ball_radius">30dp</dimen>    <dimen name="font_size">16sp</dimen></resources>

This application code retrieves a dimension:

Resources res = getResources();float fontSize = res.getDimension(R.dimen.font_size);

This layout XML applies dimensions to attributes:

<TextView    android:layout_height="@dimen/textview_height"    android:layout_width="@dimen/textview_width"    android:textSize="@dimen/font_size"/>

Related Article

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.