Android changes the way controls are laid out by code

Source: Internet
Author: User

In many cases, the way we lay out in XML does not meet our requirements, and we need to control the layout of the controls in our code.
Control the layout according to different conditions. First, let's look at some of the units in Android.

Dip:device independent pixels (device independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.
Px:pixels (pixels). Different devices show the same effect, generally we hvga represent 320x480 pixels, this use more. It is important to note that when we define the units in the XML layout file as DP, we
When the code gets its wide height. The system will return to you XP, for example through GetWidth (), and the size is PX.
Pt:point, is a standard length unit, 1pt=1/72 inches, for the printing industry, very simple to use;
sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize. As a result, according to Google's advice, TextView's
The size of the best use SP to do units, and view the source of TextView, Android by default using the SP as the font size units.
In Android, 1pt is probably equal to more than 2.22SP for reference, if the UI can provide the best design in SP, if there is no concept of SP in the design, the developer can also take approximate value by appropriate conversion.
In the past, programmers typically designed computer user interfaces in pixels. For example, you define a form field with a width of 300 pixels, a space between columns of 5 pixels, an icon size of 16x16 pixels, and so on. The problem with this approach is that if you run the program on a new monitor with a higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be too small to see the content.
Resolution-independent units of measure can solve this problem. Android supports all of the following units.
PX (pixels): The point on the screen.
In (inches): unit of length.
MM (mm): unit of length.
PT (lb): 1/72 inches.
DP (density-independent pixels): An abstract unit based on screen density. 1DP = 1px on a monitor 160 dots per inch.
Dip: Same as DP, more for android/ophone example.
SP (scale independent pixels): Similar to DP, but can be scaled based on the user's font size preference.
Resolution: The whole screen is how many points, such as 800x480, it is for the software to display units, in PX units of the point. The density (density) value represents the number of display points per inch, and the resolution is two concepts. APK in the resource bundle,

Resources that use HDPI tags when the screen is density=240
Resources that use MDPI tags when the screen is density=160
When the screen density=120, use the ldpi tag resource.
General Android Set length and width multi-use dip, set font size multi-use SP. The screen density is 160,1dp=1px=1dip, 1pt = 160/72 sp 1pt = 1/72 inches. When the screen density is 240, 1dp=1dip=1.5px.

In order for the user interface to display properly on the current and future display types, we recommend that you always use the SP as the unit of text size and dip as the unit of other elements. Of course, you can also consider using vector graphics instead of bitmaps.
Above: http://blog.csdn.net/ouyangtianhan/article/details/7245843

How do we ensure that the layout will change according to our requirements after knowing some of wirelessly's units?
As you can see from the above explanations, you want a complete adaptation screen. Then you need to understand the conversions for each unit. As already stated, the adaptation units in the XML layout file are DP-equipped. And the code gets the PX, so we need to know
The conversion method between the two units. If you want to know the conversion of these two units, you need to know the other value. That's--screen density (density)

When the screen density=240, use the HDPI label resource when the screen density is 240, 1dp=1dip=1.5px.
When the screen density=160, the resource using the MDPI tag is 160,1dp=1px=1dip on the screen density
When the screen density=120, use the ldpi tag resource.

To verify that we first define a control in the XML layout file ImageView

Set the width height to 100dp, and then we get its width in the code to see

Then we get through the code
How do you get the density of the screen?
In fact, wirelessly has provided the appropriate methods for us to obtain
Displaymetrics metric = new Displaymetrics ();
This.getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
METRIC.DENSITYDPI--------> Gets the density value of the current screen
Metric.density-----------> Gets a multiple of the screen conversion, primarily the conversion multiplier of the DP unit to the SP in XML
LOG.I ("Metric", "+metric.densitydpi+" "+metric.density");

2.0 is a multiple of the DP conversion to PX, and 320 is the density value of the screen


Now let's control the controls in the XML layout, and in Android, Android provides the Getlayoutparams () method to control the size of the sub-controls in the layout.
In other words, getlayoutparams () gets the parent layout. The child controls are then controlled through the parent layout.
Here my control parent layout is relativelayout, so relativelayout.layoutparams
If you use the wrong parent layout, you will get an error. Say type conversion exception

Android changes the way controls are laid out by code

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.