5.1 Android Basic QuickStart Layouts Relative Layout

相对布局 Relative Layout

RelativeLayout是一个ViewGroup子视图显示元素相对位置

视图的位置可以指定为相对于同级元素 (例如,向左的或给定元素的下面),或在位置相对于 RelativeLayout 领域 (例如,对齐左下中心)。

一个RelativeLayout是一个非常强大的工具,用来设计的用户界面,因为它能消除嵌套的ViewGroups。如果你发现自己用几套LinearLayout组,你就可以用单一的RelativeLayout取代它们。

  • 新建项目HelloRelativeLayout。
  • 打开res/layout/main.xml文件,如下修改

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <TextView

    android:id="@+id/label"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="Type here:"

    />

    <EditText

    android:id="@+id/entry"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:background="@android:drawable/editbox_background"

    android:layout_below="@id/label"

    />

    <Button

    android:id="@+id/ok"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_below="@id/entry"

    android:layout_alignParentRight="true"

    android:layout_marginLeft="10dip"

    android:text="OK"

    />

    <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_toLeftOf="@id/ok"

    android:layout_alignTop="@id/ok"

    android:text="Cancel"

    />

    </RelativeLayout>

    运行程序如下图:

    程序定义了一个id为label的 TextView 用于显示文字。然后定义了你个id为entry的输入框。

    android:layout_below="@id/label" 表示输入框在id为label控件的下方。然后定义了两个button按钮,第一个按钮的布局是

    android:layout_below="@id/entry" 表示在 id是entry的控件的下方,

    android:layout_alignParentRight="true" 在父元素中右对齐

    android:layout_marginLeft="10dip" 距离左边的控件10dip

       

    第二个按钮

    android:layout_toLeftOf="@id/ok" 表示在id为ok的控件的左边

    android:layout_alignTop="@id/ok" 表示和id为ok的控件上对齐

posted @ 2011-03-25 16:14  敏捷学院  阅读(214)  评论(0编辑  收藏  举报