对用五大布局对象,它们分别是FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局). FrameLayout: FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。 我们看一下效果图: 其中Main.xml 代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <!-- 我们在这里加了一个Button按钮 -->
- <Button
- android:text="button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:text="textview"
- android:textColor="#0000ff"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </FrameLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2">
- <TextView
- android:text="Welcome to Mr Wei's blog"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:text="red"
- android:gravity="center_horizontal" //这里字水平居中
- android:background="#aa0000"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="green"
- android:gravity="center_horizontal "
- android:background="#00aa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- </LinearLayout>
- </LinearLayout>
- <?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="Welcome to Mr Wei's blog:"/>
- <EditText
- android:id="@+id/entry"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- 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>
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:stretchColumns="1">
- <TableRow>
- <TextView android:layout_column="1" android:text="Open..." />
- <TextView android:text="Ctrl-O" android:gravity="right" />
- </TableRow>
- <TableRow>
- <TextView android:layout_column="1" android:text="Save..." />
- <TextView android:text="Ctrl-S" android:gravity="right" />
- </TableRow>
- <View android:layout_height="2dip" android:background="#FF909090" /> //这里是上图中的分隔线
- <TableRow>
- <TextView android:text="X" />
- <TextView android:text="Export..." />
- <TextView android:text="Ctrl-E" android:gravity="right " />
- </TableRow>
- <View android:layout_height="2dip" android:background="#FF909090" />
- <TableRow>
- <TextView android:layout_column="1" android:text="Quit"
- android:padding="3dip" />
- </TableRow>
- </TableLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:text="Welcome to Mr Wei's blog"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:layout_x="250px" //设置按钮的X坐标
- android:layout_y="40px" //设置按钮的Y坐标
- android:layout_width="70px" //设置按钮的宽度
- android:layout_height="wrap_content"
- android:text="Button"
- />
- </AbsoluteLayout>