Relativelayout is one of the most common layout methods in actual layout. Relativelayout can be used to set the positions of a view relative to other views. These positions can include the upper, lower, and left views. Therefore, it is more flexible than other layout methods.
Common attributes of relativelayout include:
Class 1: the property value is true or false.
Android: layout_centerhrizontal horizontally centered
Android: layout_centervertical vertical center
Android: layout_centerinparent is completely centered over the parent element
Android: layout_alignparentbottom mounts the lower edge of the parent element
Android: layout_alignparentleft: Stick the left edge of the parent element
Android: layout_alignparentright: Stick the right edge of the parent element
Android: layout_alignparenttop: top edge of the parent element
Android: layout_alignwithparentifmissing if the corresponding sibling element cannot be found, the parent element will be used as a reference object
Class 2: the property value must be the reference name "@ ID/ID-name" of the ID"
Android: layout_below is under an element
Android: layout_abve is above an element.
Android: layout_toleftof on the left of an element
Android: layout_torightof on the right of an element
Android: layout_aligntop: Align the top edge of an element with the top edge of an element.
Android: layout_alignleft: Align the left edge of an element with the left edge of an element.
Android: layout_alignbottom: the bottom edge of the current element is aligned with the bottom edge of an element.
Android: layout_alignright: the right edge of an element is aligned with the right edge of an element.
Category 3: attribute values are specific pixel values
Android: layout_marginbottom distance from the bottom edge of an element
Android: layout_marginleft distance from the left edge of an element
Android: layout_marginright distance from the right edge of an element
Android: layout_margintop distance from the edge of an element
The following is an example of relative layout.Activity_main.xml source code is as follows:
Android_relativelayout instance
1 < Relativelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" 2 Xmlns: Tools = "Http://schemas.android.com/tools" 3 Android: layout_width = "Match_parent" 4 Android: layout_height = "Match_parent" > 5 6 <! -- Center button: reference object --> 7 < Button 8 Android: ID = "@ + ID/mbutton_center" 9 Android: Text = "@ String/Center" 10 Android: layout_centerhorizontal = "True" 11 Android: layout_centervertical = "True" 12 Android: layout_width = "90dp" 13 Android: layout_height = "Wrap_content" > 14 </ Button > 15 16 <! -- Upper --> 17 < Button 18 Android: ID = "@ + ID/mbutton_abve" 19 Android: Text = "@ String/above" 20 Android: layout_abve = "@ ID/mbutton_center" 21 Android: layout_centerhorizontal = "True" 22 Android: layout_width = "90dp" 23 Android: layout_height = "Wrap_content" > 24 </ Button > 25 26 <! -- Lower --> 27 < Button 28 Android: ID = "@ + ID/mbutton_below" 29 Android: Text = "@ String/below" 30 Android: layout_below = "@ ID/mbutton_center" 31 Android: layout_centerhorizontal = "True" 32 Android: layout_width = "90dp" 33 Android: layout_height = "Wrap_content" > 34 </ Button > 35 36 <! -- Left --> 37 < Button 38 Android: ID = "@ + ID/mbutton_left" 39 Android: Text = "@ String/left" 40 Android: layout_toleftof = "@ ID/mbutton_center" 41 Android: layout_centervertical = "True" 42 Android: layout_width = "120dp" 43 Android: layout_height = "Wrap_content" > 44 </ Button > 45 46 <! -- Right --> 47 < Button 48 Android: ID = "@ + ID/mbutton_right" 49 Android: Text = "@ String/right" 50 Android: layout_torightof = "@ ID/mbutton_center" 51 Android: layout_centervertical = "True" 52 Android: layout_width = "120dp" 53 Android: layout_height = "Wrap_content" > 54 </ Button > 55 56 <! -- Upper left --> 57 < Button 58 Android: ID = "@ + ID/mbutton_aboveandleft" 59 Android: Text = "@ String/aboveandleft" 60 Android: layout_abve = "@ ID/mbutton_center" 61 Android: layout_toleftof = "@ ID/mbutton_abve" 62 Android: layout_width = "120dp" 63 Android: layout_height = "Wrap_content" > 64 </ Button > 65 66 <! -- Upper right --> 67 < Button 68 Android: ID = "@ + ID/mbutton_aboveandright" 69 Android: Text = "@ String/aboveandright" 70 Android: layout_abve = "@ ID/mbutton_center" 71 Android: layout_torightof = "@ ID/mbutton_abve" 72 Android: layout_width = "120dp" 73 Android: layout_height = "Wrap_content" > 74 </ Button > 75 76 <! -- Bottom left --> 77 < Button 78 Android: ID = "@ + ID/mbutton_belowandleft" 79 Android: Text = "@ String/belowandleft" 80 Android: layout_below = "@ ID/mbutton_center" 81 Android: layout_toleftof = "@ ID/mbutton_below" 82 Android: layout_width = "120dp" 83 Android: layout_height = "Wrap_content" > 84 </ Button > 85 86 <! -- Bottom right --> 87 < Button 88 Android: ID = "@ + ID/mbutton_belowandright" 89 Android: Text = "@ String/belowandright" 90 Android: layout_below = "@ ID/mbutton_center" 91 Android: layout_torightof = "@ ID/mbutton_below" 92 Android: layout_width = "120dp" 93 Android: layout_height = "Wrap_content" > 94 </ Button > 95 </ Relativelayout >
1:
Figure 1: android_relativelayout instance