Android uses code to change the layout of controls. android Layout

Source: Internet
Author: User

Android uses code to change the layout of controls. android Layout

In many cases, the layout in xml cannot meet our requirements, and we need to control the layout of controls in code.
Control the layout according to different conditions. First, let's take a look at some units in Android

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. Note that when the unit defined in the XML layout file is dp
When the code gets its width. The system will return xp, for example, the size obtained through getWidth () is px.
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. According to google's suggestion
It is best to use sp as the unit of font size, and check the source code of TextView. We can see that Android uses sp as the font size unit by default.
In Android, 1pt is about equal to 2.22sp and above for reference. If the UI can provide the best design in sp units, if the design does not have the concept of sp, then, the developer can take an approximate value by appropriate conversion.
In the past, programmers often designed computer user interfaces in pixels. For example, define a form field with a width of 300 pixels. the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem with this processing is that if you run the program on a new display with a dot (dpi) higher per inch, the user interface will look small. In some cases, the user interface may be small to difficult to see the content.
The measurement units irrelevant to resolution can solve this problem. Android supports all of the following units.
Px (pixel): the point on the screen.
In (INCHES): the unit of length.
Mm (mm): the unit of length.
Pt (lbs): 1/72 inch.
Dp (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
Dip: it is the same as dp and is mostly used in android/ophone examples.
Sp (pixels irrelevant to the scale): similar to dp, but can be scaled based on the user's font size preferences.
Resolution: the number of points on the screen, such as 800x480. It is the display unit of the software, in px. The density (density) value indicates the number of display points per inch, and the resolution is two concepts. In the apk resource package,

Resources that use the hdpi label when the screen density is 240
Resources that use the mdpi label when the screen density is 160
Ldpi tag resources are used when the screen density is 120.
Generally, android uses dip to set the length and width, and sp to set the font size. the screen density is 160, 1dp = 1px = 1dip, 1pt = 160/72 sp 1pt = 1/ 72 inch. when the screen density is 240, 1dp = 1dip = 1.5px.

To enable normal display of the current and future display types on the user interface, we recommend that you always use sp as the unit of text size and dip as the unit of other elements. Of course, you can also consider using a vector image instead of a bitmap.
From: http://blog.csdn.net/ouyangtianhan/article/details/7245843

 

After learning about some units in Android, how can we ensure that the layout can be changed according to our requirements?
The above explanation shows that the screen must be fully adapted. Then we need to understand the conversion of each unit. As mentioned above, the Adaptation unit in the XML layout file uses dp. In this way, we need to know that the pixel is obtained through code.
The conversion method between the two units. To know the conversion between the two units, you need to know the other value. That is -- screen density (density)

Resources that use the hdpi label when the screen density is 240, when the screen density is 240, 1dp = 1dip = 1.5px.
When the screen density is 160, the resource using the mdpi label is 160 in the screen density, 1dp = 1px = 1dip
Ldpi tag resources are used when the screen density is 120.

To verify that an image view control is defined in the xml layout file

Set the width and height to DP, and then we can get its width and height in the code.

In this case, we get
How can we get the screen density?
In fact, the corresponding methods have been provided in Android for us to obtain
DisplayMetrics metric = new DisplayMetrics ();
This. getWindowManager (). getDefaultDisplay (). getMetrics (metric );
Metric. densityDpi --------> obtains the density of the current screen.
Metric. density -----------> Returns a multiple of screen conversions, which are mainly the conversion multiples of the dp unit in XML to the sp.
Log. I ("metric", "" + metric. densityDpi + "" + metric. density );

Among them, 2.0 is the multiple of dp to px, and 320 is the screen density value.

 

 


Now we can control the controls in the xml layout. In Android, Android provides the getLayoutParams () method to control the size of the child controls in the layout.
That is, getLayoutParams () obtains the parent layout. Then, the Child control is controlled through the parent layout.
Here, the parent layout of my control is RelativeLayout, so RelativeLayout. LayoutParams
If an incorrect parent layout is used, an error is returned. Type conversion exception

 

 

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.