使用RelativeLayout,可以通过设置“相对位置”(每个View相对于另一个view的位置),来指定它所包含的子view的位置。看下面main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:
Android="http://schemas.
Android.com/apk/res/
Android"
Android:id="@+id/RLayout"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" >
<TextView
Android:id="@+id/lblComments"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentLeft="true"
Android:layout_alignParentTop="true"
Android:text="Comments" />
<EditText
www.atcpu.com Android:id="@+id/txtComments"
Android:layout_width="fill_parent"
Android:layout_height="170px"
Android:layout_alignLeft="@+id/lblComments"
Android:layout_below="@+id/lblComments"
Android:layout_centerHorizontal="true"
Android:textSize="18sp" />
<Button
Android:id="@+id/btnSave"
Android:layout_width="125px"
Android:layout_height="wrap_content"
Android:layout_alignRight="@+id/txtComments"
Android:layout_below="@+id/txtComments"
Android:text="Save" />
<Button
Android:id="@+id/btnCancel"
Android:layout_width="124px"
Android:layout_height="wrap_content"
Android:layout_alignLeft="@+id/txtComments"
Android:layout_below="@+id/txtComments"
Android:text="Cancel" />
</RelativeLayout>
可以观察到,这些views都被嵌在了RelativeLayout里面,每个view都有一些特有的属性去和其他view对准位置。这些属性是:
layout_alignParentTop
layout_alignParentLeft
layout_alignRight
layout_below
layout_centerHorizontal
这些属性的值,就是每个被的view的id。
效果图:
摘自 manoel的专栏