Relative Layout
If you find yourself using several nested LinearLayout
groups, you may be able to replace them with a single RelativeLayout
.
RelativeLayout
is a view group that displays child views in relative positions
android:layout_alignParentTop
If "true"
, makes the top edge of this view match the top edge of the parent.android:layout_centerVertical
If "true"
, centers this child vertically within its parent.android:layout_below
Positions the top edge of this view below the view specified with a resource ID.android:layout_toRightOf
Positions the left edge of this view to the right of the view specified with a resource ID.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="29dp" android:layout_marginTop="42dp" android:text="Button1" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button3" android:layout_alignBottom="@+id/button3" android:layout_toRightOf="@+id/button3" android:text="Button4" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:text="Button3" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button4" android:layout_alignRight="@+id/button4" android:text="Button2" /> </RelativeLayout>
android:layout_alignBaseline 对齐android:layout_alignBottom 底部对齐android:layout_toRightOf:某个组件右边android:layout_alignLeft:左边对齐android:layout_below:某个组件下面android:layout_above:某个组件上面android:layout_alignRight:右边对齐