About size and unit in Android

Source: Internet
Author: User

Tutorial 1

In the Android system, the Unit DP is dip: Device Independent pixels (device independent pixel ).

Dip: Device Independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.

Px: pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.

PT: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;

SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts.

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
The width property value is set 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.

Tutorial 2

2.1 use dimen Resources

We can use some common size units to define some text sizes, the width and height of view components. A dimension resource is a numeric data type and is defined in the res \ values \ dimens. xml file.

2.1.1 size units supported in Android

In computers, we usually use the following dimensions: centimeter (CM), millimeter (mm), pixel (PX), and feet (in. The size units supported by Android are shown in table 3.4.

Table supported units in Android

Single-bit table

Single-digit name

Single-bit description

Px

Pixels

Real pixel representation on the screen

In

Feet

Screen-based physical size

Continue table

Single-bit table

Single-digit name

Single-bit description

Mm

Millimeters

Screen-based physical size

PT

Point

1/72 feet

DP

Density-independent pixels

Abstract unit of relative screen physical density

SP

Pixels unrelated to precision

Similar to DP

2.1.2 definition of size resource XML file

A dimension resource is an integer value defined in an XML file. See table 3.5 for definitions of dimension resources.

Table 3.5 definition and use of dimension resources

Resource source location

Res/values/dimens. xml

Size XML file format

Use <? XML version = "1.0" encoding = "UTF-8"?>

<Resources> root element

<Dimen> child element: <dimen name =

Dimen_name> dimen_value </dimen>

How to obtain size Resources

Getresources (). getdimension ()

Format of referenced dimension Resource

In Java code: R. dimen. dimen_name

In the XML file: @ [Package:] dimen/dimen_name

2.1.3 use of dimension resource XML files

The following uses an instance to demonstrate the usage of dimension resources. This instance adds a textview and a button to the layout file, and uses the size resource file to define their width and height respectively.

Create a dimens. xml size resource file under the res \ values \ directory of the project.

 
 
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <dimenname="text_width">150px</dimen>
  4. <dimenname="text_height">100px</dimen>
  5. <dimenname="btn_width">30mm</dimen>
  6. <dimenname="btn_height">10mm</dimen>
  7. </resources>

Create a test_dimen.xml layout file under the res \ Layout \ directory of the project. Add a textview and a button to the layout file. Textview width and height reference size resource to set. Set the width and height of the button in the code.

 
 
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView
  6. android:text="@string/test_dimen"
  7. android:id="@+id/myDimenTextView01"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:width="@dimen/text_width"
  11. android:height="@dimen/text_height"
  12. android:background="@color/red_bg"
  13. />
  14. <Button
  15. android:text="@string/test_dimen1"
  16. android:id="@+id/Button01"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"></Button>
  19. </LinearLayout>

Create a testdimensionactivity class in the com. Amaker. dimen package. The button view component declared at the top of the class, instantiate the component in the oncreate () method, and define the size resource to set its width and height.

 
 
  1. Packagecom. Amaker. ch03.dimen;
  2. Importandroid. App. activity;
  3. Importandroid. content. res. Resources;
  4. Importandroid. OS. Bundle;
  5. Importandroid. widget. Button;
  6. Importcom. Amaker. Test. R;
  7. /**
  8. *
  9. * @ Author Guo Hongzhi
  10. * Test size Resources
  11. */
  12. Publicclasstestdimensionactivityextendsactivity {
  13. Privatebuttonmybutton;
  14. @ Override
  15. Publicvoidoncreate (bundlesavedinstancestate ){
  16. Super. oncreate (savedinstancestate );
  17. // Set the content Layout View of the current activity
  18. Setcontentview (R. layout. test_dimen );
  19. // Obtain the button instance using the findviewbyid Method
  20. Mybutton = (button) findviewbyid (R. Id. button01 );
  21. // Obtain the resources instance
  22. Resourcesr = getresources ();
  23. // Obtain the dimension value through the getdimension Method
  24. Floatbtn_h = R. getdimension (R. dimen. btn_height );
  25. Floatbtn_w = R. getdimension (R. dimen. btn_width );
  26. // Set the button width
  27. Mybutton. setheight (INT) btn_h );
  28. // Set the button height
  29. Mybutton. setwidth (INT) btn_w );
  30. }
  31. }

Run the program, as shown in result 3.4.

Figure 3.4 dimension resource application

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.