[813000] Notas - Información financiera intermedia de conformidad con la NIC 34
Nivel 1 Nivel 2 Nivel 2 Instrumentos financieros derivados
3.2
3.23.2 RelativeRelativeRelativeRelative LayoutsLayoutsLayoutsLayouts
Like linear layouts, relative layouts base the position of their controls on the metrics of the layout itself, but unlike linear layouts, the position of a control is aligned with or is an offset from any corner, edge, or the center of the layout. Controls may be aligned with other controls in the layout as well, as we shall see. This gives us considerable freedom to design our layouts, but there are a few quirks to working with relative layouts as well.
Create a new android app using any API level (for this example I'm using 4.0.3 (Ice Cream
to the res/layout/activity_main.xml file if it is not already displayed. In the XML file, make the following changes, as they will add contrast to the layout:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:background="#00FFFF" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" tools:context=".MainActivity"/> </RelativeLayout>
Notice that the TextView object, in addition to a layout_width and a layout_height attribute, now has layout_centerHorizontal and layout_centerVertical attributes as well. These are example of relative layout attributes, of which there are many. Let's return to graphical view: I've zoomed in a bit to make things clearer. In this view, notice that when the TextView is selected, the relative layout attributes are shown to the upper right of the interface display.
These correspond exactly with the xml file's attribute names. Move the text view around on the layout and observe the changing attribute names and their settings. Also note that the
alignment attributes are displayed in a tooltip while the text view is being moved.
If we move the text view to the center of anyedge of the layout, the layout attributes will be changed in such a way that there will be one parent edge attribute and one center attribute. These attributes and their values for the centers of each edge are shown in this table:
Layout Layout
LayoutLayout PlacementPlacementPlacementPlacement AttributesAttributesAttributesAttributes inininin graphicalgraphicalgraphicalgraphical viewviewviewview XMLXMLXMLXML settingssettingssettingssettings mademademademade Top, Center alignParentTop: true
centerHorizontal: true
android:layout_alignParentTop=”true” android:layout_centerHorizontal=”true” Bottom, Center alignParentBottom: true
centerHorizontal: true
android:layout_alignParentBottom=”true” android:layout_centerHorizontal=”true”
Left, Center alignParentLeft: true centerVertical: true
android:layout_alignParentLeft=”true” android:layout_centerVertical=”true” Right, Center alignParentRight: true
centerVertical: true
android:layout_alignParentRight=”true” android:layout_centerVertical=”true”
Moving the control to the corners results in these attributes being set:
Layout Layout
LayoutLayout PlacementPlacementPlacementPlacement AttributesAttributesAttributesAttributes inininin graphicalgraphical viewgraphicalgraphicalviewviewview XMLXMLXMLXML settingssettingssettingssettings mademademademade Top, Left alignParentTop: true
alignParentLeft: true
android:layout_alignParentTop=”true” android:layout_alignParentLeft=”true” Top, Right alignParentTop: true
alignParentRight: true
android:layout_alignParentTop=”true” android:layout_alignParentRight=”true” Bottom, Left alignParentBottom: true
alignParentLeft: true
android:layout_alignParentBottom=”true” android:layout_alignParentLeft=”true” Bottom, Right alignParentBottom: true
alignParentRight: true
android:layout_alignParentBottom=”true” android:layout_alignParentRight=”true”
As long as we're dealing only with edges, centers, and corners, everything is very straightforward. Now, let's move the TextView to a position near (but not actually in) the upper left corner of the layout:
The graphical view shows that the object is aligned with the top and the left sides of the layout. But if we examine the XML, we can see two new attributes:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="68dp" android:layout_marginTop="66dp" android:text="@string/hello_world" android:textColor="#000000" tools:context=".MainActivity"/>
These layout margin attributes are calculated from the edge of the control facing the nearest edge of the layout. In other words, layout_marginLeft=”68dp” means that the left edge of the control is 68density – independent pixels from the left edge of the layout, and
pixels from the the top edge of the layout. Even though the alignment is still to the parent's left and top, a margin is applied to these alignment metrics to adjust the actual position of the control.
Let's take a short detour at this point and talk about units of measure. [EDITOR: You may want to put this section (until END below) in a sidebar with the title Android Measurement Units.] There are a number of measurement units that can be applied to set positions, widths, and heights of controls. These are summarized in this chart:
Unit Unit
UnitUnit DescriptionDescriptionDescriptionDescription in
in
inin Units are given in inches, measured by the physical size of the screen mm
mm
mmmm Units are given in millimeters, measured by the physical size of the screen pt
pt
ptpt Units are in points (1 point = 1/72 inch), measured by the physical size of the screen
px px
pxpx Units are given in actual physical screen pixels dp,
dp,
dp,dp, dipdipdipdip Units are given in density – independent pixels. One density – independent pixel is equal to one pixel at 160 dots per inch. Both dp and dip are accepted; dp is preferred.
sp sp
spsp Units are given in scaled – independent pixels. One scaled – independent pixel is equal to one pixel at 160 dots per inch, but the measure is also scaled by the user's font size preference.
We can distil this information a bit: use spspspsp for anything involving text, and use dpdpdpdp for everything
else, unless actual screen pixel positions are important (such as in a game or other graphical application). In that rare case, use pxpxpxpx.
When positioning a control in graphical view, the margin attributes are set according to the nearest layout edge. For example, if the text view is positioned near the lower right corner of the layout, layout_marginRight and layout_marginBottom are used:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="80dp" android:layout_marginRight="42dp" android:text="@string/hello_world" android:textColor="#000000" tools:context=".MainActivity"/>
The arrows pointing from the control to the edges of the layout in graphical view will always indicate which margin attributes are being set in the xml file:
In the above image example, we know that layout_alignParentBottom, layout_alignParentRight, layout_marginBottom, and layout_marginRight are all being used. If the control is centered horizontally or vertically (but not both...)three attributes will be set: a layout_center... attribute, a layout_alignParent... attribute, and a layout_margin... attribute. In the following case, the text view is centered horizontally and offset a bit from the top of the layout:
Here is the corresponding xml:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="@string/hello_world" android:textColor="#000000" tools:context=".MainActivity"/>
We've already seen that changes in the graphical view are immediately set in the xml file. Changes in the xml are also reflected immediately in the graphical view. For example, if we change the xml file to read:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="34dp" android:text="@string/hello_world" android:textColor="#000000" tools:context=".MainActivity"/>
the graphical view is changed as well:
So far, we've looked at the attributes that are applied using only one control in a relative layout. Now let's see what happens when we add a second control. Center the first text view horizontally 30dp from the top of the layout, then drag a new Medium Text control to a
position below the existing text view:
The new TextView has layout_centerHorizontal applied; this should not be surprising. But the new text is aligned not with the parent, but with the previous text object. In graphical view, we can see this described as “below: textView2,” in xml view, the two text views are listed like this:
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="@string/hello_world" android:textColor="#000000" tools:context=".MainActivity"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="37dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
new text view is to be aligned vertically to the textView2 object. The layout_marginTop attribute in this case refers not to the distance between the top of the layout and the top of textView1, but to the distance between the top of textView1 and thebottom of textView2! If we move the medium text object so that its left edge is aligned with the left edge of textView2, the attributes in the xml file change to:
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/textView2" android:layout_marginTop="37dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
textView1 is still below textView2, but now the layout_centerHorizontal attribute has been replaced with layout_alignLeft=”@+id/textView2”. Note the distinction between layout_alignLeft and layout_alignParentLeft: when we align with the parent, a boolean is expected and the control will be aligned with the object that contains it. When we simply align, we are aligning to another control at the same hierarchical level, and the id of that control is expected.
There are of course many attributes that can be applied to specify the alignment of one control with respect to another. The most important are:
layout_below layout_above layout_toLeftOf layout_toRightOf layout_alignLeft layout_alignRight layout_alignTop layout_alignBottom
For a complete discussion of layout attributes applicable to the relative layout container, see the Android Documentation at
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.ht ml.
As relative layout designs become more complex you will probably find that laying out interfaces in graphical view alone will not work. Specifically, controls will tend to try to establish a layout connection to other controls even when you want that connection to be to the parent view. It therefore becomes important to be able to specify layout parameters
directly in the xml file. Make sure you take the time now to become familiar with the various layout attributes, especially as they apply to relative layouts!
A glance at the Layout folder in the palette in graphical view reveals many other forms of layout, but don’t panic... all of the other layouts in the palette are based on or derive from either a linear or relative layout. Knowing how these two types of layout work, you will easily be able to use any other form of layout.