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.
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <dimenname="text_width">150px</dimen>
- <dimenname="text_height">100px</dimen>
- <dimenname="btn_width">30mm</dimen>
- <dimenname="btn_height">10mm</dimen>
- </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.
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:text="@string/test_dimen"
- android:id="@+id/myDimenTextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="@dimen/text_width"
- android:height="@dimen/text_height"
- android:background="@color/red_bg"
- />
- <Button
- android:text="@string/test_dimen1"
- android:id="@+id/Button01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- </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.
- Packagecom. Amaker. ch03.dimen;
- Importandroid. App. activity;
- Importandroid. content. res. Resources;
- Importandroid. OS. Bundle;
- Importandroid. widget. Button;
- Importcom. Amaker. Test. R;
- /**
- *
- * @ Author Guo Hongzhi
- * Test size Resources
- */
- Publicclasstestdimensionactivityextendsactivity {
- Privatebuttonmybutton;
- @ Override
- Publicvoidoncreate (bundlesavedinstancestate ){
- Super. oncreate (savedinstancestate );
- // Set the content Layout View of the current activity
- Setcontentview (R. layout. test_dimen );
- // Obtain the button instance using the findviewbyid Method
- Mybutton = (button) findviewbyid (R. Id. button01 );
- // Obtain the resources instance
- Resourcesr = getresources ();
- // Obtain the dimension value through the getdimension Method
- Floatbtn_h = R. getdimension (R. dimen. btn_height );
- Floatbtn_w = R. getdimension (R. dimen. btn_width );
- // Set the button width
- Mybutton. setheight (INT) btn_h );
- // Set the button height
- Mybutton. setwidth (INT) btn_w );
- }
- }
Run the program, as shown in result 3.4.
|
Figure 3.4 dimension resource application |