Mind map can be found on the curtain
1. Basic
If the control does not indicate a relative position in the relative layout, the default is the upper-left corner of the relative layout:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF00FF" android:padding="20dp" android:text="Item2"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0000" android:text="Item1"/>
Gravity
gravityproperty is used to set the alignment of components within a container
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="#FF4081" android:text="Item1" />
Effect is
2. Position 2.1 Relative sibling components according to the sibling control
android:layout_above:// 参考的兄弟控件上边android:layout_below:// 参考的兄弟控件下边android:layout_toLeftOf // 参考的兄弟控件的左边android:layout_toRightOf // 参考的兄弟控件右边
code example
android:layout_belowThe attributes are selected by the control to id select the sibling component to be referenced, namely @id/firstText :
<TextView android:id="@+id/firstText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0000 " android:text="firstText" /><TextView android:id="@+id/rightText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00FF00" android:layout_toRightOf="@id/firstText" android:text="rightText" /><TextView android:id="@+id/bottomText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00FF00" android:layout_below="@id/firstText" android:text="bottomText" />
The effect shown is
2.2 Aligning relative components
There are four properties for the alignment sibling relative to the component,android:layout_align${方向}
android:layout_alignTop // 对齐参考组件的上边界android:layout_alignBottom // 对齐参考组件的下边界android:layout_alignLeft // 对齐参考组件的左边界android:layout_alignRight // 对齐参考组件的右边界
android:layout_alignTopThe same attributes are set by the control's ID to the boundary line of the referenced component:
code Example 1
<TextView android:id="@+id/item1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF00FF" android:padding="20dp" android:text="Item2"/><TextView android:id="@+id/item2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0000" android:padding="10dp" android:layout_below="@id/item1" android:layout_alignRight="@id/item1" android:text="Item1"/>
Effect is
code Example 2
<TextView android:id="@+id/item3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF00FF" android:padding="20dp" android:text="Item3"/><TextView android:id="@+id/item4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0000" android:padding="10dp" android:layout_toRightOf="@+id/item3" android:layout_alignBottom="@id/item3" android:text="Item4"/>
Effect is
3. Align 3.1 with the parent control's four edges according to the parent control's positioning
Properties that are aligned with the edge of the parent control are android:layout_alignParent${方向} composed of
android:layout_alignParentTop // 顶部对齐于父控件android:layout_alignParentBottom // 底部对齐于父控件android:layout_alignParentLeft // 左对齐于父控件android:layout_alignParentRight // 右对齐于父控件
It is important to note that these properties are set by Boolean values to align to a direction of the parent control:
<RelativeLayout android:layout_width="match_parent" android:layout_height="300dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="first" android:textSize="50dp" android:background="#FF0000" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"/></RelativeLayout>
The effect is:
In addition, there are layout_alignParentLeftlayout_alignParentTop
3.2 To the center of the Align parent control
The properties that are central to the align parent control can be used to set the center layout position:
android:layout_centerHorizontal // 水平居中android:layout_centerVertical // 垂直居中android:layout_centerInParent // 水平且垂直居中,处于父组件的正中央位置
code example
Similarly, these properties are also Boolean types by setting the value:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="centerHorizontal" android:textSize="50dp" android:background="#FF0000" android:layout_centerHorizontal="true"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="centerVertical" android:textSize="50dp" android:background="#FF0000" android:layout_centerVertical="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="centerInParent" android:textSize="15dp" android:background="#0000FF" android:layout_centerInParent="true"/>
The effect is:
4. Other baselines for align controls
<TextView android:id="@+id/firstText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="100dp" android:text="Hello" /><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/firstText" android:text="World" />
If the alignment baseline is not used, the world cannot be on the same line as Hello when the font of Hello is greater than the one:
To layout_alignBaseline align the control's baseline by adding properties to the world's TextView firstText :
android:layout_alignBaseline="@+id/firstText"
The effect is:
5. Example
Use relative layout to complete the classic plum blossom layout
<!--Central--<imageview android:id= "@+id/img1" android:layout_width= "80DP" android:layou t_height= "80DP" android:layout_centerinparent= "true" android:src= "@drawable/pic1"/> <!--right-- <imageview android:layout_width= "80DP" android:layout_height= "80DP" android:layout_centerinparent = "true" android:layout_toleftof= "@+id/img1" android:layout_centervertical= "true" android:src= "@drawab Le/pic2 "/> <!--left--<imageview android:layout_width=" 80DP "android:layout_height=" 80DP " android:layout_torightof= "@+id/img1" android:layout_centervertical= "true" android:src= "@drawable/pic3" /> <!--top--<imageview android:layout_width= "80DP" android:layout_height= "80DP" an Droid:layout_above= "@+id/img1" android:layout_centerhorizontal= "true" android:src= "@drawable/pic4"/> & lt;! ---bottom---<imAgeview android:layout_width= "80DP" android:layout_height= "80DP" android:layout_below= "@+id/img1" Android:layout_centerhorizontal= "true" android:src= "@drawable/pic5"/>
For
Resources
2.2.2 Relativelayout (relative layout)
Android Relative layout Android relative layouts detailed