Detailed explanation of absolute layout absolutelayout and relative layout examples of Android programming

This paper analyzes the absolute layout and relative layout of Android programming. Share with you for your reference, as follows:

1、 Absolute layout absolutelayout

Absolute positioning absolutelayout, also known as coordinate layout, can directly specify the absolute position of child elements. This layout is simple, direct and intuitive. However, due to the large difference of mobile phone screen size, the adaptability of using absolute positioning will be poor.

Let's take an example: the size of the robot image in the example is 250x250. You can see that we use Android: layout_ X and Android: Layout_ Y to specify the vertical and horizontal coordinates of the child elements.

XML / HTML code:

Let's take a look at the display effect under the wqvga simulator:

Then check the display effect under the wvga800 simulator:

Tip: in absolute positioning, if the child element does not set layout_ X and layout_ y. Then their default value is 0, which means that it will appear in the upper left corner as in FrameLayout.

2、 Relative layout

Relative layout relativelayout allows child elements to specify their position relative to their parent or sibling elements, which is one of the most commonly used layout methods in actual layout. It is much more flexible. Of course, there are many attributes, the operation is difficult, and the possibility of conflict between attributes is also large. When using relative layout, more tests should be done.

Let's do the above example again with relative layout. First, place a picture, and the other two texts are positioned relative to the previous element:

XML / HTML code:

Let's take a look at the display effect under the wqvga simulator:

Take another look at the display effect on the larger screen (wvga800) simulator:

It can be seen from the above figure that the interface effect is basically consistent, rather than shrinking in the upper left corner like absolute positioning; Students see that the function of automatic scaling is the benefit of using dip as unit.

Here are some important attributes used in relativelayout:

Type 1: Android: layout with attribute value of true or false_ Centerhorizontal center Android: Layout_ Centervertical center Android: Layout_ Centerinparent is completely centered relative to the parent element Android: Layout_ Align parentbottom is close to the lower edge of the parent element Android: layout_ Align parentleft is close to the left edge of the parent element Android: layout_ Align parentright is close to the right edge of the parent element Android: layout_ Align parenttop is close to the upper edge of the parent element Android: layout_ Alignwithparentifmissing if the corresponding sibling element cannot be found, the parent element is used as the reference. The second type: the attribute value must be the reference name "@ ID / ID name" of ID " Android: layout_below below an element Android: layout_above above an element Android: layout_toleftof on the left of an element Android: layout_torightof on the right of an element Android: layout_aligntop the upper edge of this element is aligned with the upper edge of an element Android: layout_alignleft the left edge of this element is aligned with the left edge of an element Android : layout_alignbottom align the lower edge of this element with the lower edge of an element Android: layout_alignright align the right edge of this element with the right edge of an element class III: attribute values are specific pixel values, such as 30dip, 40px Android: layout_marginbottom distance from the bottom edge of an element Android: layout_marginleft distance from the left edge of an element Android: layout_m Arginright distance from the right edge of an element Android: layout_margintop distance from the upper edge of an element

Let's do the above example again. This time, put more attributes in it. Let's experiment:

XML / HTML code:

The absolute layout absolutelayout and relative layout relativelayout are finished. I hope this article will be helpful to you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>